Skip to content

The const constructor #8

Open
Open
@SimonLab

Description

@SimonLab

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions