Navindra Umanee wrote:
You can accomplish nearly the same thing in Java by declaring
variables to be of type Object -- but of course it's still
strongly-typed.
"Nearly the same" means "not" in this context? I mean, if you have to declare the variables to be of a type, its quite the opposite of variables don't having any type. (And BTW there are also non-Objects in Java.)
An object in Java is typed according to its class definition and its
place in the hierarchy. If you try to do something with an object,
you must specify which type you are expecting and you must respect
that definition as well as the actual type of the object. That's type
safety by your definition.
You're misrepresenting my definition. That you have to specify, which type you're expecting, doesn't have to do anything with being type safe. You don't have to specify anything in ML either, a well-known type safe language.
If I use "strongly typed" I mean that the language is type safe: There
can never be an operation, that is applied to the wrong type. If you try this in Ruby (or Java), an exception is raised (because of dynamic type checking). So Ruby is type safe, and how I understand the phrase, "strongly typed". If you don't agree, try to define "strong typing" in another meaningful way.
As far as I can tell and what all the examples seem to have shown, is
that Ruby doesn't really have a strict concept of type as most people
understand it.
I don't know what "a strict concept of type as most people understand it" means. The result of the class method doesn't give you any hints, about the type of the objects in Ruby. That's why the "type" alternative was deprecated.
In your file-socket-example the protocol of on object changed, but this is a very ordinary thing to happen in Ruby anyway. It's easy to change the type of a single object like that:
A = Class.new; a1 = A.new; a2 = A.new
a1.methods == a2.methods #=> true
def a1.foo;end
a1.methods == a2.methods #=> false
Internally sockets and files are both abstracted as RFILEs (and integers on the C level), so it's quite easy to copy one data structure into the other. (In this case the object is still the same object like before the reopen call.) Of course this cannot be done in general in the current Ruby implementation. However you can never apply an operation to a socket or a file object that isn't supported by that object.
In Smalltalk, which is usually also considered to be a strongly typed language, it's even possible to swap any object with another, but the language is still strongly typed.
Okay, so object.class will supposedly tell you what class an object
is... but you can override that method and nobody really cares. So
Ruby doesn't guarantee anything about type safety.
Well, it does. Why are you ignoring what I am saying? It guarantees, that no operation will be executed on an object's data, that doesn't have the expected type. That's the difference to C, where this can be done without any problems (which is also quite useful to program hardware for example).
All Ruby cares about is that an object responds to the method that is
being called on it -- however that object may respond and whatever
type the object may be is irrelevant.
It's not irrelevant. If the object responds, it supports the called method, if it doesn't, it raises an exception. If you think, this is enough to make a language weakly typed, the weak/strong difference doesn't seem to be useful any more. Like I said, you could try to define "strongly typed" yourself, if you don't agree with my definition.
Duck typing seems the best description so far for what Ruby does. I
think it really confuses the issue to say that Ruby has strong typing
in addition to duck typing... unless I'm missing something, which is
very much possible.
I think you're missing something. Duck typing also explains pretty well, what happens in languages with a Hindley-Milner type system like ML. The types are inferred (here at compile time) by observing which operations are used on them. Would you say that ML isn't strongly typed?
···
--
Florian Frank