Challenge: write a reference counted slice that works as much as possible like a built-in slice
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Nov 9 00:12:10 UTC 2021
On Mon, Nov 08, 2021 at 10:15:09PM +0000, deadalnix via Digitalmars-d wrote:
[...]
> I think however, you missed several massive problems:
> 4. Type qualifier transitivity. const RCSlice!T -> const
> RCSlice!(const T) conversion needs to happen transparently.
The only way this can happen is via a language change. The only way
arrays get to enjoy such benefits is because the necessary implicit
conversion rules are baked into the language. User-defined types do not
have such privileges, and there is currently no combination of language
constructs that can express such a thing.
> 5. Head mutability. const RCSlice!(const T) -> RCSlice!(const T)
> conversion needs to happen transparently.
[...]
Ditto.
Basically, for something to be ref-counted, you need:
1) To attach a counter to the type that remains mutable in spite of the
payload being possibly const or immutable.
2) Due to D's mutable/immutable implicit conversion to const, for a
ref-counted type to avoid being cumbersome to use (e.g., pass mutable
RefCounted!T to a function receiving RefCounted!(const T) without
needing to cast / copy / any of various workarounds), a user-defined
type needs to be able to declare implicit conversions of this sort.
Current language rules do not permit this.
T
--
Fact is stranger than fiction.
More information about the Digitalmars-d
mailing list