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

Chained assignment in Scala

5 replies
E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.

Just in case someone always been missing chained assignment in Scala, here is an equivalent of ´a =
b = value´:

scala> val a@b = new Object
a: java.lang.Object = java.lang.Object@80b973
b: java.lang.Object = java.lang.Object@80b973

Notice that the variables above get initialized with the same value, as opposed to:

scala> val a,b = new Object
a: java.lang.Object = java.lang.Object@d1223d
b: java.lang.Object = java.lang.Object@1ee8c1

Also possible with ´var´s and with more than two names (but not with ´def´s):

scala> var a@(b@(c@d)) = new Object
a: java.lang.Object = java.lang.Object@cbbdf3
b: java.lang.Object = java.lang.Object@cbbdf3
c: java.lang.Object = java.lang.Object@cbbdf3
d: java.lang.Object = java.lang.Object@cbbdf3

Enjoy :)

--
EL

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: Chained assignment in Scala
Thanks a lot! Please let me use it in my kittens (scala snippets)

-Vlad


On Sun, Feb 12, 2012 at 7:57 PM, Eugen Labun <labun@gmx.net> wrote:
Just in case someone always been missing chained assignment in Scala, here is an equivalent of ´a = b = value´:

scala> val a@b = new Object
a: java.lang.Object = java.lang.Object@80b973
b: java.lang.Object = java.lang.Object@80b973

Notice that the variables above get initialized with the same value, as opposed to:

scala> val a,b = new Object
a: java.lang.Object = java.lang.Object@d1223d
b: java.lang.Object = java.lang.Object@1ee8c1


Also possible with ´var´s and with more than two names (but not with ´def´s):

scala> var a@(b@(c@d)) = new Object
a: java.lang.Object = java.lang.Object@cbbdf3
b: java.lang.Object = java.lang.Object@cbbdf3
c: java.lang.Object = java.lang.Object@cbbdf3
d: java.lang.Object = java.lang.Object@cbbdf3

Enjoy :)

--
EL

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Chained assignment in Scala

On 2012-02-13 05:32, Vlad Patryshev wrote:
> Thanks a lot! Please let me use it in my kittens (scala snippets)

Of course! (Just in case it was not a purely ritorical polite phrase)

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Chained assignment in Scala

Cool feature.
Only too bad that parentheses are needed in the last case (i.e. with 3
or more chained var assignments)
Or is this a bug?

scala> var a@b@c = new Object
:1: error: '=' expected but '@' found.
var a@b@c = new Object
^

On 13 feb, 04:57, Eugen Labun wrote:
> Just in case someone always been missing chained assignment in Scala, here is an equivalent of ´a =
> b = value´:
>
> scala> val a@b = new Object
> a: java.lang.Object = java.lang.Object@80b973
> b: java.lang.Object = java.lang.Object@80b973
>
> Notice that the variables above get initialized with the same value, as opposed to:
>
> scala> val a,b = new Object
> a: java.lang.Object = java.lang.Object@d1223d
> b: java.lang.Object = java.lang.Object@1ee8c1
>
> Also possible with ´var´s and with more than two names (but not with ´def´s):
>
> scala> var a@(b@(c@d)) = new Object
> a: java.lang.Object = java.lang.Object@cbbdf3
> b: java.lang.Object = java.lang.Object@cbbdf3
> c: java.lang.Object = java.lang.Object@cbbdf3
> d: java.lang.Object = java.lang.Object@cbbdf3
>
> Enjoy :)
>
> --
> EL

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Chained assignment in Scala

On 2012-02-14 14:31, Dave wrote:
> Cool feature.
> Only too bad that parentheses are needed in the last case (i.e. with 3
> or more chained var assignments)
> Or is this a bug?

Probably, not. IIUC the grammar doesn't allow for chained bindings.

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Chained assignment in Scala

On 2012-02-14 14:31, Dave wrote:
> Cool feature.
> Only too bad that parentheses are needed in the last case (i.e. with 3
> or more chained var assignments)
> Or is this a bug?

I looked again into the grammar, and still see no way how to come from a ´Pattern3´ (in the
´Pattern2´ rule) to a chain ´id @ ...´ (or ´varid @ ...´) to make possible a definition without
parentheses.

Here is the relevant snippet:

PatVarDef ::= ‘val’ PatDef
| ‘var’ VarDef

PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr
VarDef ::= PatDef
| ids ‘:’ Type ‘=’ ‘_’

Pattern2 ::= varid [‘@’ Pattern3]
| Pattern3
Pattern3 ::= SimplePattern
| SimplePattern { id [nl] SimplePattern }
SimplePattern ::= ‘_’
| varid
| Literal
| StableId
| StableId ‘(’ [Patterns] ‘)’
| StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ‘*’ ‘)’
| ‘(’ [Patterns] ‘)’
| XmlPattern

varid ::= lower idrest
Path ::= StableId
| [id ‘.’] ‘this’
StableId ::= id
| Path ‘.’ id
| [id ’.’] ‘super’ [ClassQualifier] ‘.’ id

--
EL

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