Re: Embedded DSLs and Productivity
Posted: Apr 13, 2006 8:18 AM
> What if the supertype is a universal type like
> We can assign all manner of different types to a variable
> typed as the universal type - just like Ruby.
> Is it easier to think of Ruby variables typed as one
> thing, and then changing to be typed as another thing, and
> then another; or is it easier to think of Ruby variables
> typed as the universal type?
Well, my point is that if the type of the variable is
Object, you can only call the methods on the object it references that are defined in class
Object. Even if the actual object is
String, you can only call the
Object methods from a variable of type
Object. To call
charAt(), you have to explicitly downcast the variable type to
String, at which point you can call the
In Ruby's case, you can call whatever method you want, and if the receiving object has a signature that matches, it will be executed.
> Bill Venners wrote
> > True, but in both cases in Java, you bind not just on
> > signature, but also on the type. In a dynamic language
> > just bind on the signature.
> iirc a Java method signature includes the parameter types
> and return type.
> In Smalltalk method lookup starts in the class of the
> object to which the message was sent, and climbs up
> through the inheritance hierarchy, trying to match the
> method selector - so binding is based on the receiver
> object class and the method selector.
Oops, good point. I need to be more precise. In fact, the JVM does a similar kind of searching up the inheritance hierarchy when it performs dynamic linking of instance methods. The difference is that in the Smalltalk case, the class of object can be anything so long as it sports a matching method signature. In the Java case, the class of the object is restricted to the type of the variable or a subtype of that variable type. Moreover, that type restriction is defined at the source code level, so you don't have to run the program to find out what that restriction is, even though you have to run the program to find out what the actual class is in each case. Because the type restriction is defined at the source code level, that information is available to static analysers.