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

Java more terse than Scala?

6 replies
nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
In Java I can do the following:

int a=45, b=74, c=11;

and

a = b = c = 0;

That doesn't seem possible in Scala. Can that be right? :-)
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Java more terse than Scala?

On Fri, Apr 03, 2009 at 08:01:23AM -0500, Nils Kilden-Pedersen wrote:
> int a=45, b=74, c=11;

scala> var (a,b,c) = (45,74,11)
a: Int = 45
b: Int = 74
c: Int = 11

This however has some subtle issues that make it not quite the same as
the individual assignments (it's treated as a tuple & pattern match.)

> a = b = c = 0;

You can't do that because assignment doesn't return the assigned value
in scala, but Unit.

Florian Hars
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?

Paul Phillips schrieb:
>> a = b = c = 0;
>
> You can't do that because assignment doesn't return the assigned value
> in scala, but Unit.

You just have to take care of your types:

scala> var (a,b,c) = ((), () ,3)
a: Unit = ()
b: Unit = ()
c: Int = 3

scala> a = b = c = 0
a: Unit = ()

Not quite what was asked, though ...

- Florian

Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?

Quoting Paul Phillips :

> On Fri, Apr 03, 2009 at 08:01:23AM -0500, Nils Kilden-Pedersen wrote:
>> int a=45, b=74, c=11;
>
> scala> var (a,b,c) = (45,74,11)
> a: Int = 45
> b: Int = 74
> c: Int = 11
>
> This however has some subtle issues that make it not quite the same as
> the individual assignments (it's treated as a tuple & pattern match.)

Exactly so, that's the reason you can't somewhere else, without
defining again, do a multiple assignment. I.e. try as next line
(a,b,c) = 0 or = (0,0,0)

>
>> a = b = c = 0;
>
> You can't do that because assignment doesn't return the assigned value
> in scala, but Unit.

Yeah and the question is: why?
why does [X]x_(x:X) => Unit and neither =>X nor =>this.type?

If you're filling a GridBagPanel.Constraints object that's a PITA at best..

-Martin

Dave Griffith
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?

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

Nils Kilden-Pedersen wrote:
>
> In Java I can do the following:
>
> int a=45, b=74, c=11;
>
> and
>
> a = b = c = 0;
>
> That doesn't seem possible in Scala. Can that be right? :-)
>
>

Jan Lohre
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?
The second is possible.

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


2009/4/3 Nils Kilden-Pedersen <nilskp@gmail.com>
In Java I can do the following:

int a=45, b=74, c=11;

and

a = b = c = 0;

That doesn't seem possible in Scala. Can that be right? :-)

Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Java more terse than Scala?

Quoting Jan Lohre :

> The second is possible.
>
> scala> val a,b,c = 0
> a: Int = 0
> b: Int = 0
> c: Int = 0

It's not. You're defining new values here, and not assigning values to
existing variables.

-Martin

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