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

Should show warning when trying to modify "const" var #1512

Closed
jaxlogan opened this issue Mar 23, 2020 · 5 comments
Closed

Should show warning when trying to modify "const" var #1512

jaxlogan opened this issue Mar 23, 2020 · 5 comments
Labels
needs-info Additional information needed from the issue author

Comments

@jaxlogan
Copy link

DartPad should show some kind of warning/error when trying to mutate a "const" declared value, such as:

const array = ["friend1", "friend2"];

void main() {
  print(array);
  array.add("friend3");
  print(array);                  // Warning/compiler error here
}
@RedBrogdon
Copy link
Contributor

Thanks for filing an issue!

Running this code in my browser is producing this error:

[friend1, friend2]
Uncaught Error: Unsupported operation: add

Is that different from what you have been seeing?

@RedBrogdon RedBrogdon added the needs-info Additional information needed from the issue author label Mar 24, 2020
@jaxlogan
Copy link
Author

jaxlogan commented Apr 6, 2020

Hi!

At DartPad, I was not able to see any error message. Perhaps it was my browser, although I was using a Chromium based one.

@no-response no-response bot removed the needs-info Additional information needed from the issue author label Apr 6, 2020
@eernstg
Copy link
Member

eernstg commented Apr 6, 2020

With respect to proposals for language support for statically known immutability, you could take a look at dart-lang/language#125 and dart-lang/language#314.

We don't have that, yet. For now it is a purely dynamic property that some collections are unmodifiable. This includes the ones that are obtained by evaluating a constant expression, as in your example.

So that's the reason why you do not get an error/warning at compile time for array.add("friend3");, but you do get an exception at run time.

I'll close this issue: Dartpad behaves as expected, and getting a compile-time error message rather than a run-time exception is a request for a language enhancement which should be discussed in the issues in the language repository like the ones mentioned above.

@eernstg eernstg closed this as completed Apr 6, 2020
@domesticmouse
Copy link
Member

@jaxlogan can you specify which chromium based browser? Chrome works as intended. Is it Brave perchance? We've had issues with that browser before.

@domesticmouse domesticmouse reopened this Apr 6, 2020
@domesticmouse domesticmouse added the needs-info Additional information needed from the issue author label Apr 6, 2020
@no-response
Copy link

no-response bot commented Apr 20, 2020

Without additional information we're not able to resolve this issue, so it will be closed at this time. You're still free to add more info and respond to any questions above, though. We'll reopen the case if you do. Thanks for your contribution!

@no-response no-response bot closed this as completed Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Additional information needed from the issue author
Projects
None yet
Development

No branches or pull requests

4 participants