Chained assignment in Scala

68 views
Skip to first unread message

Eugen Labun

unread,
Feb 12, 2012, 10:57:47 PM2/12/12
to scala-l...@googlegroups.com
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

Vlad Patryshev

unread,
Feb 12, 2012, 11:32:04 PM2/12/12
to scala-l...@googlegroups.com
Thanks a lot! Please let me use it in my kittens (scala snippets)

-Vlad

Eugen Labun

unread,
Feb 13, 2012, 12:35:04 AM2/13/12
to scala-l...@googlegroups.com
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)

Dave

unread,
Feb 14, 2012, 8:31:00 AM2/14/12
to scala-language
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
<console>:1: error: '=' expected but '@' found.
var a@b@c = new Object
^

Eugen Labun

unread,
Feb 14, 2012, 9:09:25 AM2/14/12
to scala-l...@googlegroups.com
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.

Eugen Labun

unread,
Feb 14, 2012, 4:47:00 PM2/14/12
to scala-l...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages