question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Referring to value constructors with uppercase name results in runtime errors

See original GitHub issue

I’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 switching 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:closed
  • Created 7 years ago
  • Comments:27 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
lucaswerkmeistercommented, Sep 1, 2016

“redeclare this bug as a feature”, if you prefer 😉

0reactions
gavinkingcommented, Sep 6, 2016

I could rewrite them to lowercase, but that would only work if there’s no collision, and users might be confused why the names changed.

This is exactly what you should do. We already do this kind of thing in the Java backend for lots of things.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found