This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Java more terse than Scala?

3 replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

>>>>> "Dave" == Dave Griffith writes:

Dave> I'll note that both of these are considered bad Java style. I
Dave> personally am a bit surprised that Scala allows multiple vars to
Dave> be defined with one statement, particularly since you can't
Dave> initialize them in that case.

I imagine the real goal was to support:

scala> var a,b,c = 0
a: Int = 0
b: Int = 0
c: Int = 0

and support for this:

scala> val a,b,c = 0
a: Int = 0
b: Int = 0
c: Int = 0

came as an accidental consequence?

but yeah, the latter is pretty useless :-)

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?

2009/4/3 Seth Tisue :
>>>>>> "Dave" == Dave Griffith writes:
>
>  Dave> I'll note that both of these are considered bad Java style.  I
>  Dave> personally am a bit surprised that Scala allows multiple vars to
>  Dave> be defined with one statement, particularly since you can't
>  Dave> initialize them in that case.
>
> I imagine the real goal was to support:
>
> scala> var a,b,c = 0
> a: Int = 0
> b: Int = 0
> c: Int = 0
>
> and support for this:
>
> scala> val a,b,c = 0
> a: Int = 0
> b: Int = 0
> c: Int = 0
>
> came as an accidental consequence?
>
> but yeah, the latter is pretty useless :-)

In fact it's not:

scala> var i = 0;
i: Int = 0

scala> val a, b, c = { i += 1; i }
a: Int = 1
b: Int = 2
c: Int = 3

This fact is used to good effect in Enumeration for example:

val Foo, Bar, Baz = Value

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Java more terse than Scala?

On Fri, Apr 3, 2009 at 4:52 PM, David MacIver wrote:
> 2009/4/3 Seth Tisue :
>>>>>>> "Dave" == Dave Griffith writes:
>>
>>  Dave> I'll note that both of these are considered bad Java style.  I
>>  Dave> personally am a bit surprised that Scala allows multiple vars to
>>  Dave> be defined with one statement, particularly since you can't
>>  Dave> initialize them in that case.
>>
>> I imagine the real goal was to support:
>>
>> scala> var a,b,c = 0
>> a: Int = 0
>> b: Int = 0
>> c: Int = 0
>>
>> and support for this:
>>
>> scala> val a,b,c = 0
>> a: Int = 0
>> b: Int = 0
>> c: Int = 0
>>
>> came as an accidental consequence?
>>
>> but yeah, the latter is pretty useless :-)
>
> In fact it's not:
>
> scala> var i = 0;
> i: Int = 0
>
> scala> val a, b, c = { i += 1; i }
> a: Int = 1
> b: Int = 2
> c: Int = 3
>
> This fact is used to good effect in Enumeration for example:
>
> val Foo, Bar, Baz = Value
>
Right. AFAIRC the motivation for multiple initializations was to make
enumerations work smoothly.

Cheers

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: Java more terse than Scala?

>>>>> "David" == David MacIver writes:

David> In fact it's not:

scala> var i = 0;
David> i: Int = 0

scala> val a, b, c = { i += 1; i }
David> a: Int = 1 b: Int = 2 c: Int = 3

Cool! That never occurred to me to try. It would be nice if Java
worked that way too, actually. But in Java this:

int a, b, c = 5 ;

initializes c to 5 and leaves a and b uninitialized.
The legacy of C is still with us...

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland