Issue 23523 - Allow User-Defined Implicit Conversions for const/immutable containers
Summary: Allow User-Defined Implicit Conversions for const/immutable containers
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-30 14:57 UTC by John Hall
Modified: 2022-12-17 10:31 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description John Hall 2022-11-30 14:57:31 UTC
From here (among other places) on the forum
https://forum.dlang.org/post/ooraoriyrftjrpoygtnk@forum.dlang.org

Allow `const type!T` to convert to `type!(const T)`
Similarly, allow `immutable type!T` to convert to `type!(immutable T)`
Comment 1 John Hall 2022-11-30 18:21:11 UTC
In the past the argument from Walter against something like opImplicitCast has always been that users would get confused about random things happening if they had freedom to do any implicit cast they could think of.

The solution to this would be only to allow the implicit conversions discussed above. So instead of some general opImplicitCast it might be better described as opImplicitConstCast.
Comment 2 Nick Treleaven 2022-12-04 18:19:34 UTC
> Similarly, allow `immutable type!T` to convert to `type!(immutable T)`

That isn't always safe:

struct S(T)
{
    int* p;
}

void main()
{
    auto s = S!(immutable int)();
    immutable si = S!int();
    s = si; // currently an error
    *s.p = 4;
    assert(*si.p == 0); // fails
}