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

Dummy val bindings in blocks _

4 replies
Florian Haftmann
Joined: 2010-06-14,
User offline. Last seen 32 weeks 4 days ago.

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

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Dummy val bindings in blocks _

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...
>
>

Mark Harrah
Joined: 2008-12-18,
User offline. Last seen 35 weeks 3 days ago.
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...
> >
> >
>
>

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
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...
>> >
>> >
>>
>>
>
>

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
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...
>>> >
>>> >
>>>
>>>
>>
>>
>

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