Referring to value constructors with uppercase name results in runtime errors
See original GitHub issueI’ve discovered that if a value constructor is referred to in uppercase syntax, the JS backend emits ClassName$c_ConstName
instead of ClassName$c_ConstName()
(missing parentheses). This means that the value you get at runtime is a function instead of a proper object. When switch
ing on the object, cases written in lowercase syntax will not match.
shared class C of \iA {
shared String x = "x";
shared new \iA {}
}
shared void run() {
// without \i, no ()s are emitted
C c1 = C.A;
switch (c1)
case (C.A) { print("A1"); } // works, but ===s the function instead of the actual value
switch (c1)
case (C.\iA) { print("A2"); } // doesn’t work
print(c1.x); // prints "<null>"
// with \i, ()s are emitted
C c2 = C.\iA;
switch (c2)
case (C.A) { print("A3"); } // doesn’t work
switch (c2)
case (C.\iA) { print("A4"); } // works properly
print(c2.x); // prints "x"
}
(You can get the same effect with a lowercase constructor name and \I
references.)
I’m not sure why it’s legal to refer to the value constructor by UIdentifier (edit: perhaps #6099, as in #6287?), but as initial uppercase names seem to be the convention for TypeScript enums (which correspond to classes with value constructors), it would be preferrable to keep this legal.
Issue Analytics
- State:
- Created 7 years ago
- Comments:27 (26 by maintainers)
Top Results From Across the Web
Compile and Runtime Errors in Java
ECLIPSE: Void methods cannot return a value. Every method except constructors must have a return type or void specified; if not, this error ......
Read more >Why do data constructors have a capital letter? : r/haskell
To me, I've always thought that data constructors should have a lowercase first letter, because they are term-level and behave…
Read more >BP CH 7 - Classes & Objects Flashcards - Quizlet
a. By convention class names begin with an uppercase letter, and method and variable names begin with a lowercase letter. b. Instance variables...
Read more >Fix the top 10 most common compile time errors in Java
Example of an 'unreachable code' error, one of several common Java compile errors. 1. Class and source file mismatch. The name of the...
Read more >Javascript "Not a Constructor" Exception while creating objects
As an unrelated side node, general JavaScript style guidelines recommend starting a variable identifier with an uppercase letter only if it is a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
“redeclare this bug as a feature”, if you prefer 😉
This is exactly what you should do. We already do this kind of thing in the Java backend for lots of things.