Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

val 1 = 2 compiles #4939

Closed
scabug opened this issue Aug 23, 2011 · 5 comments
Closed

val 1 = 2 compiles #4939

scabug opened this issue Aug 23, 2011 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Aug 23, 2011

The following code should not compile.

scala> object Foo { val 1 = 2 }
defined module Foo

Also, as been pointed out by Seth Tisue, the defined val shadows the corresponding integer literal, producing unexpected behavior:

scala> object O { val 1 = 2; def x = 1 }
defined module O

scala> O.x
scala.MatchError: 2 (of class java.lang.Integer)

@scabug
Copy link
Author

scabug commented Aug 23, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4939?orig=1
Reporter: @lexn82
Affected Versions: 2.9.0-1, 2.9.1

@scabug
Copy link
Author

scabug commented Aug 23, 2011

@paulp said:
"val 1 = 2" compiling is according to spec.

PatVarDef ::= ‘val’ PatDef
PatDef    ::= Pattern2 ‘=’ Expr
Pattern2  ::= Literal

It's not so according to spec that one can redefine the literal 1.

@scabug
Copy link
Author

scabug commented Aug 23, 2011

@paulp said:
Oh yeah, that's the object constructor throwing that exception. In that case it's all as expected.

@scabug scabug closed this as completed Aug 23, 2011
@scabug
Copy link
Author

scabug commented Aug 24, 2011

@SethTisue said (edited on Aug 24, 2011 2:29:27 PM UTC):
Indeed, val definitions that don't bind any variables are actually explicitly speced. See SLS 4.1: "If p has no bound variables..."

There is much discussion of this issue at https://groups.google.com/forum/#!topic/scala-user/k57U6jt8Za0

Surely this is a bug though:

Welcome to Scala version 2.9.1.RC4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> { val 1 = 2 }
scala.MatchError: 2 (of class java.lang.Integer)

scala> val 1 = 2

scala> 

No error there at the end, just "OK boss!"

Shall I open a separate ticket on it? Or shall we reopen this one?

@scabug
Copy link
Author

scabug commented Aug 29, 2011

@SethTisue said:
I opened #4950 on the REPL part of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants