jastrachan | 7 Jul 15:37 2004

Re: [groovy-dev] static binding subsystem for better performance

One minor thought...

On 7 Jul 2004, at 13:42, bing ran wrote:
> Thanks for saying that.
> To give the compiler the hint for static binding, here are the things 
> you
> can do:
> 1. Try set the specific data type when defining a field, variable etc, 
> but
> most of the time this is not necessary, since the compiler will 
> attempt to
> infer the data type in the assignment chain:
> String a = "some thing"
> b = a + " more"  // b is a String
> c = b.length()	// c is an Integer, because .length() is statically
> dispatched and the return type is known at compile time, which then is 
> set
> on c.
> 2. There is no need to specify the data type when you  declare a new
> variable and initialize it with a "new" object instance or a constant, 
> eg,
> myFile = new File("fiename"")
> name = "Bing Ran"
> age = 36
> male = true
> The compiler can infer the data type of the variable. The rule of 
> thumb is:
> if you can infer the data type by reading the code, the compiler 
> usually
> can.
> 3.  If you set the datatype of a variable to Object, you're telling the
> compiler not to anything about it. Any method invocation on the object 
> will
> be dispatched dynamically.

In static typing mode we should be checking the methods are correct at 
compile time & giving a nice error if they're not - this is one of the 
main aims of static typing afterall. (Whether we're in static typing or 
dynamic typing, the compiler/runtime can get clever & do static method 
dispatch but I consider that an optimisation).  So if you declare a 
variable to be of type Object and then call some method foo() it should 
probably barf & fail to compile rather than quietly going into dynamic 
typing mode.

Maybe we should use a magic type name of 'any' to indicate that you 
definitely want to be dynamically typed?