T
thufir
Is it because Ruby is dynamic, or something else?
-Thufir
-Thufir
Is it because Ruby is dynamic, or something else?
Is it because Ruby is dynamic, or something else?
-Thufir
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.
Cheers
robert
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Yeah, thats why I was talking about "such functionality". It would
serve the same purpose. I was not clear enough about that.
thufir said:Is it because Ruby is dynamic, or something else?
thufir said:Is it because Ruby is dynamic, or something else?
-Thufir
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]
Exactly.
All this doesn't make any sense in Ruby, as there is no real 'compile
time'.
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.
It wouldn't be useful to have some sort test to make sure there's not a
type problem ahead of time?
-Thufir
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<String> 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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.