From: Florian Gilcher Date: 2008-04-20T19:52:35+09:00 Subject: Re: why doesn't ruby have generics? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Apr 20, 2008, at 7:15 AM, thufir wrote: > On Sun, 20 Apr 2008 07:49:02 +0900, Joseph Lenton wrote: > >> As far as I've understood it, in Ruby it's not about what the type is >> but what the type can do. i.e. does it respond to a specific >> method. So >> maybe generics in Ruby would make sense if you could restrict objects >> based on what they can do. For example an array which can only hold >> obejct that respond to the to_str method. > > Right; I'm taking a Java course which will be covering Generics and, > in > Java, this a big part of the point of Generics: > > an array which can only hold objects which implement a specified > interface. > > > -Thufir > > Arrays are not the interesting thing when it comes to Generics. (As Arrays in Java already force every Element to be of the same type) The interesting thing about Generics in Java (and the point where they cannot make sense in ruby) is that they are only checked on _compile_ time. So, at runtime, all generic collections behave as a collection of Object. The difference between Generics and Templates in C++ is that C++ generates one Class for each used Template+Type while Java only generates one (that actually doesn't care about the type anymore). This technique is called "Type erasure", because the type information gets erased after it was compiled.[1] All this doesn't make any sense in Ruby, as there is no real 'compile time'. Regards Florian Gilcher [1]: http://en.wikipedia.org/wiki/Generics_in_Java#Type_erasure -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkgLIGwACgkQJA/zY0IIRZafYwCfUgILOhKowyEzxYZ/ToTBT/1c RLsAn2b295Qx2dDSDXcr8ZipjxeLjOQ6 =dK1x -----END PGP SIGNATURE-----