Final type qualifier
vit
vit at vit.vit
Tue Jul 23 07:15:16 UTC 2024
On Thursday, 18 July 2024 at 10:27:29 UTC, Quirin Schroll wrote:
> 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.
Nice idea. Having `final` pointer will by nice.
Some little problems:
`const` is type qualifier and method qualifier.
`final` is already a method qualifier with different meaning.
Some other new keyword will be better, for example `sealed` or
other (method qualifier will be nice too).
What does `final(ClassType)` do? Is it like `final(T)*`,
`final(T*)` or `final(final(T)*)`? (special handling of reference
types is very annoying in D )
```
void foo(final(T*) x);
void foo(const(T*) x);
void main(){
T* ptr;
foo(ptr); //what is called and why?
}
```
Is needed to specify implicit qualifier conversions from and to:
`final`
`final inout`
`final shared`
`final shared inout`
Try expand:
https://dlang.org/spec/const3.html#combining_qualifiers
https://dlang.org/spec/const3.html#implicit_qualifier_conversions
More information about the dip.ideas
mailing list