jastrachan | 29 Nov 13:11 2004

Re: variable declaration syntax: use cases and thoughts

On 29 Nov 2004, at 11:51, Jochen Theodorou wrote:
> jastrachan@... schrieb:
>> On 28 Nov 2004, at 15:01, Jochen Theodorou wrote:
> [...]
>>> for (var int i=0; i<I_MAX; i++) {
>>>  /* do something */
>>> }
>>> I don't like this one... and saying "var" is not needed if a type is 
>>> given seems to be confusing... why is it sometime optional and 
>>> sometimes not. Being able to control the behaviour is different I 
>>> think. But it seems currently "var" is just a better "any"... better 
>>> because var is not that misleading as any.
>>> So I'm still not sure if it is worth it.
>> Note that 'var' is only required for non-typed variables. So its not 
>> required in your example above. So if you want to adorn your variable 
>> declarations with type information like 'int' then you don't need to 
>> use 'var'/
> I know, please read the quoted text again. "var" says "I want to 
> declare a variable". So why do I have to say this, if there is no type 
> given and why do I not to have to say this, if there is a type given?

We have 2 options. We follow ruby/pythons' lead and do away with 
declarations for non-typed variables (i.e. if no static type is 
specified) - or we introduce a lexical token to indicate a variable 
declaration. Using a variable declaration syntax has benefits as it 
catches out types when a variable name is typed wrongly.

So using the 'var' token seems the simplest solution.

> "var" is no type.

No. 'var' just means a variable declaration.

>  But you want to use it as a type.

No I don't :)

> This is a mix I don't like. The same with "def". "any" would be 
> different because it was intended to be some kind of type, but a 
> keyword saying "definition" (def, var, dim, ...) is not recognized as 
> type. You would not say "any int i=0" because it doesn't make much 
> sense. Saying "var int i=0" and "int i=0" is the same is confusing. 
> Why should I use var if a type is given?

You shouldn't.

> How about finding a better name for "any" than a variable declaration 
> token? maybe... "dyn", "dynamic", "dyntype", "dynclass", "obj" or 
> simply "Object"... ok, "obj" and "Object" are no choice if there is 
> really a difference from object.
> list = new ArrayList()
> print list.size()
> in 99% of the cases I want an ArrayList, so why not say the compiler 
> to automatically get the type:
> auto list = new ArrayList()
> I like this for variable delcarations but it is not good for methods 
> so why not use "dyn" or "dynamic"?
> With this I can say
> dyn list = ArrayList()
> Object o = list
> print list.size()
> print o.size()
> should cause a compile time error in the 2. print because o is of type 
> Object but not in the first print because the type will be determined 
> by interference mechanisms

Seems like all you're doing is arguing against the choice of 'var' as 
the token with a collection of suggestions (auto, dyn, obj). So far I'm 
liking 'var' better; as it means variable declaration and doesn't 
confuse folks with special types (any/dyn/obj/auto).

i.e. a variable declaration in Groovy is...

variableDeclaration = [ 'var' | typeExpression ] vanillaName [ '=' 
expression ]