Generic open type syntax

View: New views
5 Messages — Rating Filter:   Alert me  

Generic open type syntax

by Mike Nichols :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,
I am trying to get the Type of an object that has an open generic
declaration:
So for example in C#:
bool c = typeof(IMyInterface<>).IsAssignableFrom(...)

What is the appropriate syntax for this?

Thank you
Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Generic open type syntax

by Cedric Vivier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Fri, Jun 20, 2008 at 1:31 PM, Mike Nichols <nichols.mike.s@...> wrote:
> I am trying to get the Type of an object that has an open generic
> declaration:
> So for example in C#:
> bool c = typeof(IMyInterface<>).IsAssignableFrom(...)
>
> What is the appropriate syntax for this?

Hi!

IIRC, it's typeof(IMyInterface[of *])

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Generic open type syntax

by Avishay Lavie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


The syntax is IMyInterface[of *].

However, note that IsAssignableFrom, when called on a generic
definition, will most always return false. Generic type definitions do
not inherit from generic type definitions directly, but rather from an
open constructed version of the base definition, using the derived
type's generic type parameters.

Suppose we have this scenario:
class Base[of T]:
  pass

class Derived[of T] (Base[of T]):
  pass

Then, it is true that for any type X, Base[of
X].IsAssignableFrom(Derived[of X]). But it is NOT true that Base[of
*].IsAssignableFrom(Derived[of *]).
What you're looking for is this:

derived = typeof(Derived[of *])
base = typeof(Base[of *])
print base.IsAssignableFrom(derived) # false!

derivedT = derived.GetGenericArguments()[0] # this is the generic
parameter T declared in Derived[of T]
baseOfDerivedT = base.MakeGenericType(derivedT) # this is Base[of T]
where T is the generic parameter declared in Derived
print baseOfSameT.IsAssignableFrom(derived) # true!

This might make more sense if you changed Derived's type parameter
name from T to U. Then you'd have:

class Derived[of U] (Base[of U])

which clearly illustrates that Derived inherits from a constructed
version of Base, using its own U as argument (rather than Base's T).

On Jun 20, 8:31 am, Mike Nichols <nichols.mik...@...> wrote:

> Hi,
> I am trying to get the Type of an object that has an open generic
> declaration:
> So for example in C#:
> bool c = typeof(IMyInterface<>).IsAssignableFrom(...)
>
> What is the appropriate syntax for this?
>
> Thank you
> Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Generic open type syntax

by Mike Nichols :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks to both of you and for the explanation! After scratching my
head for a while I realized IsAssignableFrom was not appropriate for
what I was trying to do due to the reasons you explained.

On Jun 20, 2:55 am, Avish <some.av...@...> wrote:

> The syntax is IMyInterface[of *].
>
> However, note that IsAssignableFrom, when called on a generic
> definition, will most always return false. Generic type definitions do
> not inherit from generic type definitions directly, but rather from an
> open constructed version of the base definition, using the derived
> type's generic type parameters.
>
> Suppose we have this scenario:
> class Base[of T]:
>   pass
>
> class Derived[of T] (Base[of T]):
>   pass
>
> Then, it is true that for any type X, Base[of
> X].IsAssignableFrom(Derived[of X]). But it is NOT true that Base[of
> *].IsAssignableFrom(Derived[of *]).
> What you're looking for is this:
>
> derived = typeof(Derived[of *])
> base = typeof(Base[of *])
> print base.IsAssignableFrom(derived) # false!
>
> derivedT = derived.GetGenericArguments()[0] # this is the generic
> parameter T declared in Derived[of T]
> baseOfDerivedT = base.MakeGenericType(derivedT) # this is Base[of T]
> where T is the generic parameter declared in Derived
> print baseOfSameT.IsAssignableFrom(derived) # true!
>
> This might make more sense if you changed Derived's type parameter
> name from T to U. Then you'd have:
>
> class Derived[of U] (Base[of U])
>
> which clearly illustrates that Derived inherits from a constructed
> version of Base, using its own U as argument (rather than Base's T).
>
> On Jun 20, 8:31 am, Mike Nichols <nichols.mik...@...> wrote:
>
> > Hi,
> > I am trying to get the Type of an object that has an open generic
> > declaration:
> > So for example in C#:
> > bool c = typeof(IMyInterface<>).IsAssignableFrom(...)
>
> > What is the appropriate syntax for this?
>
> > Thank you
> > Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Generic open type syntax

by Avishay Lavie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You're welcome. If my guess is right and you're trying to figure out
if a given type inherits from a constructed version of a generic type
definition ("does X implement some form of IEnumerable<>?"), Boo has a
FindConstructedTypes method somewhere in TypeSystemServices or
GenericsServices which does exactly that. I don't know any better way
to do this.

On Jun 20, 7:05 pm, Mike Nichols <nichols.mik...@...> wrote:

> Thanks to both of you and for the explanation! After scratching my
> head for a while I realized IsAssignableFrom was not appropriate for
> what I was trying to do due to the reasons you explained.
>
> On Jun 20, 2:55 am, Avish <some.av...@...> wrote:
>
> > The syntax is IMyInterface[of *].
>
> > However, note that IsAssignableFrom, when called on a generic
> > definition, will most always return false. Generic type definitions do
> > not inherit from generic type definitions directly, but rather from an
> > open constructed version of the base definition, using the derived
> > type's generic type parameters.
>
> > Suppose we have this scenario:
> > class Base[of T]:
> >   pass
>
> > class Derived[of T] (Base[of T]):
> >   pass
>
> > Then, it is true that for any type X, Base[of
> > X].IsAssignableFrom(Derived[of X]). But it is NOT true that Base[of
> > *].IsAssignableFrom(Derived[of *]).
> > What you're looking for is this:
>
> > derived = typeof(Derived[of *])
> > base = typeof(Base[of *])
> > print base.IsAssignableFrom(derived) # false!
>
> > derivedT = derived.GetGenericArguments()[0] # this is the generic
> > parameter T declared in Derived[of T]
> > baseOfDerivedT = base.MakeGenericType(derivedT) # this is Base[of T]
> > where T is the generic parameter declared in Derived
> > print baseOfSameT.IsAssignableFrom(derived) # true!
>
> > This might make more sense if you changed Derived's type parameter
> > name from T to U. Then you'd have:
>
> > class Derived[of U] (Base[of U])
>
> > which clearly illustrates that Derived inherits from a constructed
> > version of Base, using its own U as argument (rather than Base's T).
>
> > On Jun 20, 8:31 am, Mike Nichols <nichols.mik...@...> wrote:
>
> > > Hi,
> > > I am trying to get the Type of an object that has an open generic
> > > declaration:
> > > So for example in C#:
> > > bool c = typeof(IMyInterface<>).IsAssignableFrom(...)
>
> > > What is the appropriate syntax for this?
>
> > > Thank you
> > > Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price