An idiom for disabling implicit conversions

Uranuz via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 27 02:39:59 PDT 2014


On Tuesday, 26 August 2014 at 18:29:49 UTC, Uranuz wrote:
> On Tuesday, 26 August 2014 at 16:48:08 UTC, Timon Gehr wrote:
>> On 08/26/2014 05:46 PM, Uranuz wrote:
>>> In the pre-last paragraph please read text^
>>> Also notice that C is language with weak type system but D is
>>> declared to have type system.
>>>
>>> as:
>>> Also notice that C is language with weak type system but D is
>>> declared to have *strong* type system.
>>
>> It's not _that_ strong. Also, this is a library problem.
>>
>> I'd suggest you file a bug report/enhancement request against 
>> Phobos, if it is not already there.
>> https://issues.dlang.org/
>>
>> It seems that adding some constructor(s)/opAssign(s) to 
>> Nullable will solve this problem.
>>
>> ( BTW: You are more likely to get a quick response if your 
>> post looks a little bit more digestible, like:
>>
>> On 08/26/2014 05:38 PM, Uranuz could have written:
>>> // ---
>>> import std.typecons;
>>> void main(){
>>>   Nullable!ubyte x;
>>>   Nullable!int y=x; // AssertError: Called 'get' on null 
>>> Nullable!ubyte
>>> }
>>> // ---
>>> import std.typecons;
>>> void main(){
>>>   Nullable!ubyte x;
>>>   Nullable!int y;
>>>   y=x; // AssertError: Called 'get' on null Nullable!ubyte
>>> }
>>> // ---
>>>
>>> Is this a bug?
>>
>> Or you could have gone straight to https://issues.dlang.org/. 
>> :) )
>
> The last example is even more obvious but didn't come to my 
> mind. Maybe I'l think of better implementation myself and push 
> something to standard library.
>
> The best way to make something working is to do it myself 
> [jokingly]


If I could write multiple alias this in Nullable I could create 
implicit conversions from Nullable!byte not only to byte but also 
to Nullable!short, Nullable!int. In this case there is no need to 
implement lots of overloads for property of type Nullable!int in 
order to correctly accept Nullable!byte, Nullable!ubyte and so 
on. I have wrote an example code and seems that only fixing 
Nullable implementation will not help to eliminate the problem I 
was talking about.

So I have a choice:
1. Implement Nullable type without *alias this* at all (and 
access to internal value with getter method or property)
2. Implement lots of overloads of virtual Nullable!int property 
to accept every minor integer types.

Also is needed to forbid Nullable!uint conversion because it is 
incorrect. I can't correctly store uint.max inside int variable, 
but implicit coversion is allowed in the compiler (and it is not 
documented).


Because using alias this makes code shorter I prefer it, although 
it is very tricky feature and needs attention during development.


More information about the Digitalmars-d mailing list