Final type qualifier

Quirin Schroll qs.il.paperinik at gmail.com
Thu Jul 18 10:27:29 UTC 2024


Add the `final` type to the language. It is head-`const` like in 
Java. Also, make type qualifiers to class references refer to the 
object, not the reference, except `final`.

That is, `const(final(int)[])` is `const(int[])` because const 
goes down transitively and supersedes `final`. Similarly, 
`final(const(int[]))` is the same as `const(int[])`, but a 
`final(const(Object))` isn’t just `const(Object)` as the latter 
is assignable.

If a class object is accessed via a `const` reference, the 
language must add both `final` and `const` to the result.

This solves two unrelated issues:

- `const` class objects can’t be referred to by assignable 
handles. It requires some trickery which probably breaks the type 
system. Generally, if a function returns a non-mutable class 
object, it’s not justified why the caller must be unable to 
re-assign the variable the result is put in.

- `const` delegates are broken as they don’t respect `const` 
guarantees. As of today, `const` delegates are factually `final` 
delegates: They can’t be reassigned, but their context may change 
through the context pointer that’s part of the delegate. If we 
had `final` in a new edition, old-edition `const` delegates could 
become `final` delegates in the new edition and the new edition 
could fix `const` delegates.




More information about the dip.ideas mailing list