Replies: 5 comments
-
Will higher kinded polymorphism (#339) help you? |
Beta Was this translation helpful? Give feedback.
-
Yeah, I've read that post before. The idea of HKP indeed focused on the same aspect of generics with what I described above, but I wish to call it generics on generics as the author @gafter of that post subtitled which is easier to understand. But some kind of different. In my opinion, HKP uses So, the point of HKP is to generate:
And the point of
Once you get The code above is just one example in a scenario of serializer implementation, in fact we encountered many scenarios that can make use of |
Beta Was this translation helpful? Give feedback.
-
@qaqz111 It's the same problem from two different standpoints, since in both cases the CLR has to support generic parameters having generic parameters of their own. Right now you cannot express this, so there's no way to extract the parameter during the execution. |
Beta Was this translation helpful? Give feedback.
-
Java wildcards? |
Beta Was this translation helpful? Give feedback.
-
There is another trick:
(not disagreeing that it would be nice to have some attention paid to generics though) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is an old question.
As you can see, the 2nd
Write
will never be called unless you explicitly convert the object toIEnumerable<XXX>
.Previously there is an issue suggested allow
not
constraint to generics to address this problem, but it seems that there is a very long way to go for that.For now, the solution should look like this:
If there is a way to extract
type parameters
of ageneric type parameter
as atype parameter
, we can do this:The point is
typeparameterof(T, 0)
, which extracts the 1st type argument fromobj
's type.Yes, for this particular scenario, there is a trick to play with:
But, if you think this trick can solve the problem perfectly, you are too young too simple.
It looks like this big piece of code would be the only solution for now:
So, I think we need operators to extract information about generic types:
And, these operators should work with array types too, although they are not considered as generic type by compiler for now.
If the
typeparameterscountof(T)
can be implemented, the code will be more simpler:Of course the literal name of operators can be simplified, like
tparamof()
/tparam()
,tcountof()
/tcount()
, etc.Beta Was this translation helpful? Give feedback.
All reactions