implicit construction operator

Rubn where at is.this
Tue Feb 27 21:11:14 UTC 2018


On Monday, 26 February 2018 at 19:32:44 UTC, ketmar wrote:
> WebFreak001 wrote:
>
>> Now before you would have only been able to do this:
>>
>> ---
>> Nullable!Foo a;
>> foo(a, Nullable!int(5));
>> ---
>>
>> but now you should also be able to do:
>>
>> ---
>> Nullable!Foo x = null;
>> Nullable!Foo y = 5;
>>
>> foo(null, 5);
>
> please no. such unobvious type conversions goes out of control 
> really fast. there is a reason why D doesn't have such thing, 
> this is not an oversight, but a design decision.

The bigger mistake is D allowing implicit conversion when a type 
is declared, with no way to disable it when it wasn't asked for.

struct SomeStruct
{
     this(int)
     {
     }
}

SomeStruct value = 10; // I didn't want this

int oops();
SomeStruct value2 = oops(); // Didn't want this either.

SomeStruct ok() { return SomeStruct(10); }
SomeStruct value2 = ok(); // this is what I wanted

On the other hand I want to create a "null" like value for my own 
types. This currently isn't possible in D, because there is no 
implicit conversion allowed for structs. You can be explicit and 
declare every type, but "null" wouldn't be as convenient if you 
had to specify the type it has to convert to every time you 
wanted to use it. I can only imagine the hell hole DMD's source 
code would be if that was the case, with its excessive use of 
null.

You can make the same argument about mixin's, it's possible to 
create some really nasty code with it. That is unreadable and 
unmaintainable, it's especially a pain in the ass when you have 
to debug such code. Yet, there it is in D. Most useful features 
have the capability of being misused, that doesn't mean we 
shouldn't use them.


More information about the Digitalmars-d mailing list