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

can't refer to variable with symbolic name without explicit prefix #1696

Closed
scabug opened this issue Feb 6, 2009 · 11 comments
Closed

can't refer to variable with symbolic name without explicit prefix #1696

scabug opened this issue Feb 6, 2009 · 11 comments

Comments

@scabug
Copy link

scabug commented Feb 6, 2009

scala> [paulp@jon trunk (lazyrepl)]$$ scala27
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val & = "abc"
&: java.lang.String = abc

scala> &
     | ;
<console>:2: error: illegal start of simple expression
       ;
       ^

scala> val x = &
     | ;
<console>:2: error: illegal start of simple expression
       ;
       ^

scala> $$amp
res0: java.lang.String = abc

(Assigning to myself, ticket for my reference.)

@scabug
Copy link
Author

scabug commented Feb 6, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1696?orig=1
Reporter: @paulp
Duplicates #8918

@scabug
Copy link
Author

scabug commented Apr 24, 2009

@paulp said:
Note to self: this isn't exactly a mangling issue. There doesn't seem to be any way at all to refer to a val called & or ! or other confusing names without using a path, and since vals defined in the interpreter are stuffed into an anonymous object, that route is unavailable.

@scabug
Copy link
Author

scabug commented Sep 17, 2009

@paulp said:
This behaves differently now. & works:

scala> val & = "abc"
&: java.lang.String = abc

scala> &
res0: java.lang.String = abc

scala> val x = &
x: java.lang.String = abc

But ! doesn't, and with backticks at least it really should:

scala> val ! = 5  
!: Int = 5

scala> !
     | ;
<console>:2: error: illegal start of simple expression
       ;
       ^

scala> val x = !;
<console>:1: error: illegal start of simple expression
       val x = !;
                ^

scala> val x = `!`;
<console>:1: error: illegal start of simple expression
       val x = `!`;
                  ^

scala> val `!` = 10
!: Int = 10

This works in a pinch:

scala> $$bang
res2: Int = 10

scala> val p = $$bang
p: Int = 10

Lowering priority.

@scabug
Copy link
Author

scabug commented May 31, 2011

@LilyLambda said:
I'm not sure what the interpreter has to do with this.

object Tilde {
  val ~ = 5
  println(~)
}
yuvi@types$ scalac Tilde.scala 
Tilde.scala:3: error: illegal start of simple expression
  println(~)
           ^
Tilde.scala:4: error: ')' expected but '}' found.
}
^
two errors found
yuvi@types$ scalac -version
Scala compiler version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

@scabug
Copy link
Author

scabug commented May 31, 2011

@paulp said:
As I noted in a comment above yours, albeit perhaps unclearly, it is not an interpreter issue per se, but the way it manifests in the repl is more of a problem because you cannot easily work around it with the full path when the full path involves a secret hidden object.

scala> object ~ { val x = 5 }
defined module $tilde
scala> ~.x
<console>:1: error: illegal start of simple expression
       ~.x
        ^
// can't really expect people to figure this one out
scala> $line27.$read.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.~.x
res4: Int = 5

@scabug
Copy link
Author

scabug commented May 31, 2011

@LilyLambda said:
I was suggesting that the category be changed from "Interpreter and scripts" to "compiler".

@scabug
Copy link
Author

scabug commented May 31, 2011

@paulp said:
There are categories? OK!

@scabug
Copy link
Author

scabug commented Jul 22, 2011

@jrudolph said:
I think you could quite easily fix the parser so that it would accept +,-,~,! as identifiers as a simple expression. However, that wouldn't be usable in many cases because this introduces a possible ambiguity between a name being meant as a prefix operator or as an identifier.

E.g. what should be the meaning of

val + = 12
+ + + // should that mean (+).+(+) or the invalid (+ +).+

@scabug
Copy link
Author

scabug commented Jul 23, 2011

@LilyLambda said:
Good point, but I think a one-line addition to the spec would settle this question. Assuming it isn't covered already. I don't think it will come up much, so as long as there's some set rule, I don't see it as a big barrier.

@scabug
Copy link
Author

scabug commented Jun 11, 2012

@adriaanm said:
I'm calling wontfix since it seems to be of limited scope.
I suspect people experimenting in the repl would either chose a different name or put the one-who-shall-not-be-referenced-without-prefix in a prefix that can be written down:

scala> object foo { object ~ }
defined module foo

scala> foo.~
res4: foo.~.type = foo$$tilde$@560e018a

Community, feel free to reopen (preferable with a pull request in hand).

@scabug scabug closed this as completed Jun 11, 2012
@scabug
Copy link
Author

scabug commented Jun 2, 2015

@som-snytt said:
The examples work now.

The triple plus is not invalid:

scala> val + = 12
+: Int = 12

scala> + + +
<console>:9: error: ambiguous reference to overloaded definition,
both method + in class Int of type (x: Char)Int
and  method + in class Int of type (x: Byte)Int
match expected type ?
              + + +
                  ^

scala> val f: Int => Int = + + +
warning: there was one feature warning; re-run with -feature for details
f: Int => Int = <function1>

The rule is that it takes the first plus in the traditional way if it can.

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

1 participant