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

The const constructor #8

Open
SimonLab opened this issue Jan 17, 2020 · 0 comments
Open

The const constructor #8

SimonLab opened this issue Jan 17, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@SimonLab
Copy link
Member

I recently blocked on a line similar to:

final myList = const [1,2,3];

I wan't sure about the meaning of const when initialising a final variable.
In this situation const is used to instanciate (create) a new immutable object.
This means that the list object [1, 2, 3] can't be changed, and the following code won't work:

final myList = const [1, 2, 3];
myList.add(4); // error

If we ignore const then we are able to use the function add on the list, and this following code will work:

final myList = [1, 2, 3]
myList.add(4) // myList contains now the value [1, 2, 3, 4]

ref:

@SimonLab SimonLab added the enhancement New feature or request label Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant