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

Better control over immutability #189

Closed
jclark opened this issue Jun 5, 2019 · 6 comments
Closed

Better control over immutability #189

jclark opened this issue Jun 5, 2019 · 6 comments
Labels
enhancement Enhancement to language design lang Relates to the Ballerina language specification

Comments

@jclark
Copy link
Collaborator

jclark commented Jun 5, 2019

The goals are:

  • to be able create immutable values without having to do a (deep) copy
  • to be able to have variables (particularly at module level) whose values are computed at runtime, but are guaranteed immutable and so can be accessed safely from multiple threads

Note that we can already do the above for compile-time constants. The goal here is to extend this to values that are not known at runtime.

It is not a goal to guarantee at compile-time that immutable values are not written to. Some instances of this are caught at compile-time, but in general this is enforced at runtime. This is consistent with the overall design of the type system which guarantees that reads succeed and are type-safe, but does not make the same guarantee for writes. It is also consistent with what we do with const.

This could work as follows.

  • Allow the following things to be explicitly declared as immutable:
    • variables
    • parameters
    • return values
  • When something is declared as immutable, its type is constrained to be declared as a subtype of anydata|error
  • Allow type-cast-expr to cast to immutable e.g. <immutable>

We could also use readonly instead of immutable.

Note that this does not allow types to be declared immutable, nor fields of records/objects.

Semantics are as follows:

  • at compile time each expression has not only a type but mutability
  • the mutability of an expression is either “immutable”, meaning “known to be immutable”, or unspecified meaning “not known whether it it is immutable or not”
  • if the compile time mutability of an expression is “immutable”, it is guaranteed that at runtime the value of that expression will be immutable
  • contextually expected type would generalize into something that also has contextually expected mutability (so for example, in return E the contextually expected mutability of E would come from the declared mutability of the return type of the function
  • the type cast works with mutability exists as it works with type: it both sets the contextually expected mutability downwards at compile time, and verifies the mutability of the value upwards at runtime
  • since type cast works with immutability, dynamic type test should also i.e. E is immutable

To explain this, it would help to have a word that means type (in the sense of shape) together with immutability. This could be called “type”, where type has two aspects, which are shape and immutability. However that does not fit with how we are already using the keyword “type” to describe just the shape aspect of type.

This is the second part of #186. It also relates to #145.

@jclark jclark added enhancement Enhancement to language design lang Relates to the Ballerina language specification labels Jun 5, 2019
@jclark jclark added this to the 2019Rn milestone Jun 5, 2019
@jclark jclark mentioned this issue Jun 13, 2019
@jclark
Copy link
Collaborator Author

jclark commented Jun 13, 2019

Give we are replacing freeze with readOnlyClone (see #186), the right keyword will be readonly rather than immutable.

@jclark
Copy link
Collaborator Author

jclark commented Jun 15, 2019

Do we also need to say for a parameter that the immutability of the result comes from the immutability of an a parameter?

@jclark jclark changed the title Construct values as immutable without cloning Be able to declare variables as having immutable values Jun 15, 2019
@jclark
Copy link
Collaborator Author

jclark commented Jun 18, 2019

We would want the values of annotations to be constructed as immutable.

@jclark
Copy link
Collaborator Author

jclark commented Feb 28, 2020

There is now a public proposal which covers this issue and #338. It is substantially different from what is described in the first comment on this issue.

@jclark
Copy link
Collaborator Author

jclark commented Mar 26, 2020

jclark added a commit that referenced this issue Mar 26, 2020
Equivalent to union of inherently immutable basic types.
No type parameter.
Fixes #462. First step of #189.
@jclark
Copy link
Collaborator Author

jclark commented Mar 28, 2020

I separated the part of this relating to objects into #469.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to language design lang Relates to the Ballerina language specification
Projects
None yet
Development

No branches or pull requests

1 participant