digitalmars.D - Implicit conversions of head-const to tail-const ranges
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (5/5) Mar 23 2021 Has there been any attempts in dmd to generalize implicit
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/4) Mar 23 2021 There's already toHeadMutable defined in dmd which is likely part
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (16/21) Mar 23 2021 Do we want such a change to affect
- Paul Backus (8/13) Mar 23 2021 As far as I know the main reason this hasn't been done is that it
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/7) Mar 24 2021 I don't quite follow what this refers to. Can you give a code
- Paul Backus (10/17) Mar 24 2021 struct Example(T)
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (11/16) Mar 25 2021 I presented a method that would be mostly drop-in on the blog
Has there been any attempts in dmd to generalize implicit conversions to allow implicit conversion of a head-const to tail-const range? Similar to what already happens for dynamic arrays (slices). Are there any apparent show-stoppers?
Mar 23 2021
On Tuesday, 23 March 2021 at 16:50:24 UTC, Per Nordlöw wrote:Has there been any attempts in dmd to generalize implicitThere's already toHeadMutable defined in dmd which is likely part of a potential solution.
Mar 23 2021
On Tuesday, 23 March 2021 at 16:50:24 UTC, Per Nordlöw wrote:Has there been any attempts in dmd to generalize implicit conversions to allow implicit conversion of a head-const to tail-const range? Similar to what already happens for dynamic arrays (slices). Are there any apparent show-stoppers?Do we want such a change to affect safe unittest { import std.algorithm.iteration : map; import std.stdio; auto x = [1, 2]; const y = x.map!(_ => _*_); writeln(y); } currently printing const(MapResult!(__lambda2, int[]))([1, 2]) to instead print [1, 4] ? I guess not.
Mar 23 2021
On Tuesday, 23 March 2021 at 16:50:24 UTC, Per Nordlöw wrote:Has there been any attempts in dmd to generalize implicit conversions to allow implicit conversion of a head-const to tail-const range? Similar to what already happens for dynamic arrays (slices). Are there any apparent show-stoppers?As far as I know the main reason this hasn't been done is that it requires user-defined implicit conversions (something like "opHeadMutable"), which Walter is generally opposed to. You can't do a naive `const(Range!T)` -> `Range!(const(T))` conversion, because there may be `static if` statements (or other reflection) inside the `Range` template that cause incompatible structs to be generated for `T` and `const(T)`.
Mar 23 2021
On Tuesday, 23 March 2021 at 20:44:03 UTC, Paul Backus wrote:You can't do a naive `const(Range!T)` -> `Range!(const(T))` conversion, because there may be `static if` statements (or other reflection) inside the `Range` template that cause incompatible structs to be generated for `T` and `const(T)`.I don't quite follow what this refers to. Can you give a code reference or sample?
Mar 24 2021
On Wednesday, 24 March 2021 at 10:25:41 UTC, Per Nordlöw wrote:On Tuesday, 23 March 2021 at 20:44:03 UTC, Paul Backus wrote:struct Example(T) { static if (isMutable!T) T t; else T* ptr; } It would be incorrect to allow `const(Example!int)` to implicitly convert to `Example!(const(int))`.You can't do a naive `const(Range!T)` -> `Range!(const(T))` conversion, because there may be `static if` statements (or other reflection) inside the `Range` template that cause incompatible structs to be generated for `T` and `const(T)`.I don't quite follow what this refers to. Can you give a code reference or sample?
Mar 24 2021
On Tuesday, 23 March 2021 at 16:50:24 UTC, Per Nordlöw wrote:Has there been any attempts in dmd to generalize implicit conversions to allow implicit conversion of a head-const to tail-const range? Similar to what already happens for dynamic arrays (slices). Are there any apparent show-stoppers?I presented a method that would be mostly drop-in on the blog last year: https://dlang.org/blog/2020/06/25/a-pattern-for-head-mutable-structures/ It would require some (additive) changes to Phobos to be useful, and was intended as a DIP until I was informed there was a plan for these conversions. I believe any solution would look similar to my solution, but there could be other options. Maybe I should look into the DIP route again. -- Simen
Mar 25 2021