- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Dummy val bindings in blocks _
Thu, 2010-07-29, 11:05
Hi all,
I stumbled over an interesting behaviour of _ in val bindings:
scala> { val _ = 42; val _ = 1705; 123 }
error: _ is already defined as value _
{ val _ = 42; val _ = 1705; 123 }
^
Here, _ seems to be treated as an anonymous variable: double assignment
is rejected.
scala> { val _ = 42; val (_, _) = (1705, 1705); 123 }
res: Int = 123
The _s in a pattern binding however are treated just as dummy placeholders.
Is there a deeper reason for this distinction?
Cheers,
Florian
Fri, 2010-07-30, 01:47
#2
Re: Dummy val bindings in blocks _
On Thursday, July 29, 2010, Stefan Langer wrote:
> Actually _ is a valid variable name so if you do a second val _ the
> compiler will complain as you are trying to redeclare an existing
> variable.
The underscore is not a valid identifier according to the spec (1.1). It is a reserved word. The compiler agrees in other cases where there isn't the possibility of a pattern. For example:
scala> def _ = 42
:1: error: identifier expected but '_' found.
def _ = 42
^
scala> class _
:1: error: identifier expected but '_' found.
class _
^
It looks like a bug to me.
-Mark
>
> When doing val(_,_) you are actually calling a pattern matcher -
> Tuple2.unapply -- I think
>
> -Stefan
>
> 2010/7/29 Florian Haftmann :
> > Hi all,
> >
> > I stumbled over an interesting behaviour of _ in val bindings:
> >
> > scala> { val _ = 42; val _ = 1705; 123 }
> > error: _ is already defined as value _
> > { val _ = 42; val _ = 1705; 123 }
> > ^
> >
> > Here, _ seems to be treated as an anonymous variable: double assignment
> > is rejected.
> >
> > scala> { val _ = 42; val (_, _) = (1705, 1705); 123 }
> > res: Int = 123
> >
> > The _s in a pattern binding however are treated just as dummy placeholders.
> >
> >
> > Is there a deeper reason for this distinction?
> >
> >
> > Cheers,
> > Florian
> >
> > --
> >
> > Home:
> > http://www.in.tum.de/~haftmann
> >
> > PGP available:
> > http://home.informatik.tu-muenchen.de/haftmann/pgp/florian_haftmann_at_i...
> >
> >
>
>
Fri, 2010-07-30, 09:47
#3
Re: Dummy val bindings in blocks _
then I do not understand this from the REPL
scala> val _ = 1
_: Int = 1
this should be a bug then
-Stefan
2010/7/30 Mark Harrah :
> On Thursday, July 29, 2010, Stefan Langer wrote:
>> Actually _ is a valid variable name so if you do a second val _ the
>> compiler will complain as you are trying to redeclare an existing
>> variable.
>
> The underscore is not a valid identifier according to the spec (1.1). It is a reserved word. The compiler agrees in other cases where there isn't the possibility of a pattern. For example:
>
> scala> def _ = 42
> :1: error: identifier expected but '_' found.
> def _ = 42
> ^
>
> scala> class _
> :1: error: identifier expected but '_' found.
> class _
> ^
>
> It looks like a bug to me.
>
> -Mark
>
>>
>> When doing val(_,_) you are actually calling a pattern matcher -
>> Tuple2.unapply -- I think
>>
>> -Stefan
>>
>> 2010/7/29 Florian Haftmann :
>> > Hi all,
>> >
>> > I stumbled over an interesting behaviour of _ in val bindings:
>> >
>> > scala> { val _ = 42; val _ = 1705; 123 }
>> > error: _ is already defined as value _
>> > { val _ = 42; val _ = 1705; 123 }
>> > ^
>> >
>> > Here, _ seems to be treated as an anonymous variable: double assignment
>> > is rejected.
>> >
>> > scala> { val _ = 42; val (_, _) = (1705, 1705); 123 }
>> > res: Int = 123
>> >
>> > The _s in a pattern binding however are treated just as dummy placeholders.
>> >
>> >
>> > Is there a deeper reason for this distinction?
>> >
>> >
>> > Cheers,
>> > Florian
>> >
>> > --
>> >
>> > Home:
>> > http://www.in.tum.de/~haftmann
>> >
>> > PGP available:
>> > http://home.informatik.tu-muenchen.de/haftmann/pgp/florian_haftmann_at_i...
>> >
>> >
>>
>>
>
>
Fri, 2010-07-30, 09:57
#4
Re: Dummy val bindings in blocks _
And just to proof it
scala> println (`_`)
1
-Stefan
2010/7/30 Stefan Langer :
> then I do not understand this from the REPL
>
> scala> val _ = 1
> _: Int = 1
>
> this should be a bug then
>
> -Stefan
>
> 2010/7/30 Mark Harrah :
>> On Thursday, July 29, 2010, Stefan Langer wrote:
>>> Actually _ is a valid variable name so if you do a second val _ the
>>> compiler will complain as you are trying to redeclare an existing
>>> variable.
>>
>> The underscore is not a valid identifier according to the spec (1.1). It is a reserved word. The compiler agrees in other cases where there isn't the possibility of a pattern. For example:
>>
>> scala> def _ = 42
>> :1: error: identifier expected but '_' found.
>> def _ = 42
>> ^
>>
>> scala> class _
>> :1: error: identifier expected but '_' found.
>> class _
>> ^
>>
>> It looks like a bug to me.
>>
>> -Mark
>>
>>>
>>> When doing val(_,_) you are actually calling a pattern matcher -
>>> Tuple2.unapply -- I think
>>>
>>> -Stefan
>>>
>>> 2010/7/29 Florian Haftmann :
>>> > Hi all,
>>> >
>>> > I stumbled over an interesting behaviour of _ in val bindings:
>>> >
>>> > scala> { val _ = 42; val _ = 1705; 123 }
>>> > error: _ is already defined as value _
>>> > { val _ = 42; val _ = 1705; 123 }
>>> > ^
>>> >
>>> > Here, _ seems to be treated as an anonymous variable: double assignment
>>> > is rejected.
>>> >
>>> > scala> { val _ = 42; val (_, _) = (1705, 1705); 123 }
>>> > res: Int = 123
>>> >
>>> > The _s in a pattern binding however are treated just as dummy placeholders.
>>> >
>>> >
>>> > Is there a deeper reason for this distinction?
>>> >
>>> >
>>> > Cheers,
>>> > Florian
>>> >
>>> > --
>>> >
>>> > Home:
>>> > http://www.in.tum.de/~haftmann
>>> >
>>> > PGP available:
>>> > http://home.informatik.tu-muenchen.de/haftmann/pgp/florian_haftmann_at_i...
>>> >
>>> >
>>>
>>>
>>
>>
>
Actually _ is a valid variable name so if you do a second val _ the
compiler will complain as you are trying to redeclare an existing
variable.
When doing val(_,_) you are actually calling a pattern matcher -
Tuple2.unapply -- I think
-Stefan
2010/7/29 Florian Haftmann :
> Hi all,
>
> I stumbled over an interesting behaviour of _ in val bindings:
>
> scala> { val _ = 42; val _ = 1705; 123 }
> error: _ is already defined as value _
> { val _ = 42; val _ = 1705; 123 }
> ^
>
> Here, _ seems to be treated as an anonymous variable: double assignment
> is rejected.
>
> scala> { val _ = 42; val (_, _) = (1705, 1705); 123 }
> res: Int = 123
>
> The _s in a pattern binding however are treated just as dummy placeholders.
>
>
> Is there a deeper reason for this distinction?
>
>
> Cheers,
> Florian
>
> --
>
> Home:
> http://www.in.tum.de/~haftmann
>
> PGP available:
> http://home.informatik.tu-muenchen.de/haftmann/pgp/florian_haftmann_at_i...
>
>