Why is D unpopular, redux.
Timon Gehr
timon.gehr at gmx.ch
Wed May 25 00:13:58 UTC 2022
On 25.05.22 01:40, Walter Bright wrote:
> On 5/24/2022 4:21 PM, Timon Gehr wrote:
>> For containers: Just consider a struct wrapping a built-in slice.
>>
>> struct Slice(T){
>> T[] slice;
>> }
>>
>> T[] is a subtype of const(T)[], but Slice!T is not a subtype of
>> Slice!(const(T)). In general this cannot be guaranteed, but containers
>> may need to be able to allow this kind of conversions.
>
> What about:
>
> struct Slice(T) { T[] slice; int* p; }
>
> ? A const applied to T will not affect int*, but if the const
> automatically was transferred to Slice, it would, and that would be
> wrong. It seems you're asking for a special case, and as we all lament,
> special cases lead to corner cases with special problems.
> ...
Not really. What's needed is some feature that _allows_ implementing a
correct slice wrapper. I am not asking for the slice wrapper above to do
the right thing by default.
>
>> For ranges:
>>
>> E.g., here, x is a const slice and can be mapped. The resulting const
>> value cannot be used as a range anymore.
>>
>>
>> ```d
>> import std.algorithm;
>> void main(){
>> const x=[1,2,3,4];
>> const y=x.map!(v=>2*v); // ok
>> const z=y.map!(v=>2*v); // error
>> }
>> ```
>>
>> This kind of thing should just work, but there is no way to strip away
>> the topmost const for a MapResult.
>
> const(int**) p;
> cast()(p) produces a const(int*)*
>
> I.e. cast() strips the "head" qualifiers.
> ...
It often strips more than that.
struct S(T){ T* p; }
const(S!int) s;
auto t=cast()s; // this is just an S!int, but stripping the "head"
qualifiers would give us something like an S!(const(int)).
I am not suggesting `cast()` should behave like this by default, because
as you point out, it is not what you want in general. But there is a
problem.
More information about the Digitalmars-d
mailing list