- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Chained assignment in Scala
Mon, 2012-02-13, 04:57
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
Mon, 2012-02-13, 06:41
#2
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)
Tue, 2012-02-14, 14:41
#3
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
Tue, 2012-02-14, 15:21
#4
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.
Tue, 2012-02-14, 22:51
#5
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
-Vlad
On Sun, Feb 12, 2012 at 7:57 PM, Eugen Labun <labun@gmx.net> wrote: