16 Nov 2004 15:26
Re: Keyword: "any" over "def"
I confess to preferring 'def' in the JSR conference, particularly as
its got widespread use in other languages like Ruby and Python and
we're already using it today for defining functions outside of classes.
But the use of 'any' does simplify the language syntax somewhat.
e.g.
variable declaration is
VariableDeclaration = Type VanillaName
Type = JavaClassName | 'any'
Which is much simpler than
VariableDeclaration = Type VanillaName | 'def' VanillaName | 'def' Type
VanillaName
Also it removes an unnecessary option...
def String x = "foo"
So it must only be
String x = "foo"
i.e. there should only be 2 ways of declaring 'x',
String x = "foo"
any x = "foo"
depending on if you want static or dynamic typing; not 3 depending on
if you want to use 'def' or not.
I'm still a little on the fence; but 'any' is feeling simpler & cleaner.
On 16 Nov 2004, at 14:00, Laforge Guillaume wrote:
> Hi,
>
> To recap: in order to lower the ambiguity factor, one of the things
> that emerged during the JSR meeting is to always define the type of a
> variable, or use the def keyword. The same is true for defining
> functions/methods in scripts or classes. Thus:
>
> - defining new unitialized variables or fields:
>
> Integer a
> String b
> def c
> a = 1
> b = "hello"
> c = "whatever"
>
> - defining initialized variables or fields:
>
> Integer a = 1
> String b = "Hello"
> def c = 2
> def Integer d = 3
>
> The following forms will generating compiler errors:
>
> c
> c = "whatever"
> c = 2
>
> For defining methods or functions:
>
> def myMethod() { ... }
> String myMethod() { ... return "" }
>
> But this one would be illegal:
>
> myMethod() { ... }
> // this could potentially be used for defining macros
>
> I think instead of using "def" to define fields/variables/methods, we
> should require that a type be defined, either through a specific type,
> such as String, Integer, or Object, or by using the "any" keyword,
> which would mean "any type".
> The distinction is the following: if you choose to be statically
> typed, specify a type, and if you want to be dynamically typed, use
> "any" as a type.
> I tend to prefer the idea of an "any" keyword, compared to "def".
> It has the advantage of being more consistent. Let's see why with some
> other examples:
>
> - defining uninitialized variables/fields:
>
> String foo
> Integer i
> any bar
> foo = "wombat"
> i = 45
> bar = "gizmo"
>
> - defining intialized variables/fields:
>
> String foo = "wombat"
> Integer i = 45
> any bar = "gizmo"
>
> - defining methods:
>
> void someMethod() { ... }
> any someOtherMethod() { ... }
> String someStringMethod() { ... }
>
> Advantages:
> - The "look" is more consistent, you always need a type, either any or
> specific type.
> - It's clearer what a method returns (def someMethod() returns
> something? or is void?).
> - It's more concise when defining an initialized variable: String foo
> = "bar" vs def String foo = "bar".
> - Dynamical typing is easily differentiated by the use of "any", and
> statical typing with a specific type definition.
>
>
> So, "any" or "def" ?
> My favourite is "any"!
>
> --
> Guillaume Laforge
> http://glaforge.free.fr/weblog
>
>
James
-------
http://radio.weblogs.com/0112098/
RSS Feed