-
Notifications
You must be signed in to change notification settings - Fork 21
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
Case classes exhibit bizarre, order-dependent behavior when members are backquoted #8831
Comments
Imported From: https://issues.scala-lang.org/browse/SI-8831?orig=1
|
Dayton Williams (LrdDimwit) said: If you run the preceding in a debugger, you will see that both fields of w have the value 1. However, if you change the order of declaration of the fields in this case class - the 'right' class - then this behavior disappears, and the values are set as you would expect. I admit I'm a bit of a scala newbie, so there may be some caveat I don't know about saying "don't do that". This minimized test case actually comes from using json4s to parse JSON, the keys of which contain spaces. This condition came up in some of the JSON I was trying to parse, and I finally tracked down the causes of my failing tests to be this issue. |
@som-snytt said: scala> case class wrong(`a b`: Int, a: Int)
defined class wrong
scala> wrong(1, 2)
res6: wrong = wrong(1,1)
scala> .`a b`
res7: Int = 1
scala> res6.a
res8: Int = 1
scala> .`a beans Plus, code completion tab after the b yields what you see here. But there's an issue for backticks in REPL. |
Dayton Williams (LrdDimwit) said: Using the same scala interpreter I used before: scala> case class verywrong( [...] I have attached the full log of the session. |
@refried said:
|
Jens Moeller (jensmoeller) said: The source of the issue seems to be: https://github.com/scala/scala/blob/2.11.x/src/compiler/scala/tools/nsc/transform/Constructors.scala#L501, where the wrong parameter can be matched when the names involved include dollar signs (even when the names used in the source program do not include dollar signs). The current behaviour matches names that are exactly the same (which is correct) as well as names that start with the same substring, followed by a dollar sign, followed by anything. That behaviour is somewhat weird, but would explain the different test cases, since the space in I looked into it, and it seems the current behaviour was changed in this commit: scala/scala@6b6d214#diff-086eb87150c79730cf3cfe9cb8b12219L70, where the previous behaviour was to match names that are exactly the same as well as names that start with the same substring and with a dollar sign at the end. Reverting the behaviour to that in the commit solves most cases discussed here (as well as the case "case class plus(a_+ : Int, a_ : String)"): Only Arya Irani's case with "case class right5(b: Int, I did not figure out what the purpose of the check is. The original behaviour seems to be to check against an "original name": scala/scala@cb7711d#diff-086eb87150c79730cf3cfe9cb8b12219L58. In either case, I think a refactoring of the handling of Names from working on the string values to encapsulating the string value and instead provide more descriptive methods would be useful in making it much easier to verify and maintain correctness in relation to the handling of names, though I can imagine that would be quite a lot of work. In line with this, a better fix than presented below would implement a descriptive method (describing what the matching means) on Name (or similar) that handles the matching and call that instead of directly matching on parts of the string value of the name. The potential fix (tested using "ant test-opt"): def matchesName(param: Symbol) =
param.name == name ||
(param.name.startsWith(name) && param.name.endsWith(nme.NAME_JOIN_STRING)) |
@retronym said: In the commit comment, I describe the rationale for I'll make sure we fix this before 2.11.7. Until then, the workaround is to avoid using case class param names that start with |
@retronym said:
|
@melezov said: |
@adriaanm said: |
found again in #11212 |
Found again a flavor of it in 2.12.6:
This should output "ab" then "abc", but outputs "abc" and "abc"... |
another slight variant at #11704 |
Experienced a manifestation of this issue when using variable names that contains hyphens within backticks inside case classes.
|
With this ticket closed, we'll just have to devise some new ways to make Scala programmers question their own sanity! |
Don't tempt me Glad to see it finally got closed! |
Scala version used: 10.4.2
I have encountered what I am fairly sure is broken behavior in case classes when members are declared using backquotes.
I have the following minimized test case:
The text was updated successfully, but these errors were encountered: