From: Robert Klemme Date: 2008-04-19T21:45:08+09:00 Subject: Re: why doesn't ruby have generics? On 19.04.2008 14:04, Florian Gilcher wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > > On Apr 19, 2008, at 1:45 PM, Robert Klemme wrote: > >> On 19.04.2008 12:06, Florian Gilcher wrote: >>> On Apr 19, 2008, at 11:58 AM, thufir wrote: >>>> Is it because Ruby is dynamic, or something else? >>> Why would you bind to a type in a language that doesn't really >>> care about types? Otherwise, it is easy to implement such >>> functionality - just build a new kind of array that has to be >>> constructed with a class. Check for this class on insertion. >>> I don't see the use, though. >> That would be a type restricted Array but not a generic Array. You >> cannot have generics in a language whose variables are typeless as >> Arlen pointed out. > > Yeah, thats why I was talking about "such functionality". It would > serve the same purpose. I was not clear enough about that. For practical purposes you are probably right. But strictly speaking there is a difference: Java Generics basically are a mechanism for automated casting. This is something else than restricting the type of items you put into a collection. (You can sneak an Integer into a List in Java.) C++ templates are a completely different story - although they "look" pretty similar to Java's Generics. C++ templates allow for generic programming which is something different altogether. In Ruby you can do neither: you cannot cast because variables are typeless. And you cannot have generic algorithms for the same reason. Kind regards robert