-
Notifications
You must be signed in to change notification settings - Fork 240
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
Immutability of class and boxed enum instances is not enforcable #1213
Comments
alternatively jakt could model c++ in allowing mutable class references to be assigned to immutable variables. that would mean
|
regarding the first point jakts "safety by default" design guide i would assume that class "values" would be const by default and would have to be declared mut: |
const return values are meaningless in C++, so it would be difficult to enforce any sort of const correctness that way in either the generated C++ code or any external C++ code calling jakt directly. Correct reference semantics of class and boxed types is definitely an area that could use improvement though, in more ways than this particular issue. |
They are not meaningless i the way i mean. So the thing is class "values" in jakt are actually shared Pointers, i.e. have reference semantics. In that case there are two things that can be const: the reference and the referenced instance, in jakt that is currently treated together it seems, i.e. if you have an immutable variable of class "value" you can neither change the reference nor the referenced value. So if you do the same for function returns it would mean that returning "const class values" mean you cannot use this to change the referenced class instance. And that is definitely needed to enforce immutability of class instances.
Can you elaborate? |
Mutability in jakt is modeled around value semantics, i.e. if one copies an immutable value such as a number or a struct instance the copy can be mutable:
which is totally fine. However classes and boxed enums have reference semantics, so chaning the "copy" will also changed the "copied" value, because it was not in fact copies. To fix this two things would have to be changed:
for the latter again I see three possibilities
const
qualifier for return typesmut
qualifierthe latter could actually be combined with one of the other two
To really make using immutable instances safe imho it would have to be guarantueed that all variables holding a reference to an instance are immutable. That means either an instance would have to be immutable at construction time and then it should also not be possible to assign it to a mutable variable. or assigningments chaning mutability would have to assert that the reference count is 1.
The text was updated successfully, but these errors were encountered: