- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Java more terse than Scala?
Fri, 2009-04-03, 14:01
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? :-)
int a=45, b=74, c=11;
and
a = b = c = 0;
That doesn't seem possible in Scala. Can that be right? :-)
Fri, 2009-04-03, 14:47
#2
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
Fri, 2009-04-03, 14:57
#3
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
Fri, 2009-04-03, 15:27
#4
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? :-)
>
>
Fri, 2009-04-03, 15:37
#5
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>
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? :-)
Fri, 2009-04-03, 15:47
#6
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
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.