The year is 2019

Exil Exil at gmall.com
Wed Jul 31 01:54:55 UTC 2019


On Sunday, 28 July 2019 at 02:22:36 UTC, Manu wrote:
> On Sat, Jul 27, 2019 at 5:45 PM Exil via Digitalmars-d 
> <digitalmars-d at puremagic.com> wrote:
>>
>> On Saturday, 27 July 2019 at 22:36:23 UTC, Manu wrote:
>> > I think implicit conversions are EXTREMELY enabling for a 
>> > lot of cases of meta-programming in D, and I also feel much 
>> > more comfortable with the idea that they are opt-in rather 
>> > than opt-out as in C++. I think that's the right balance.
>>
>> I think it'd be even worse with meta programming. Now not only 
>> do you not know what type you may have, case it can be any 
>> type. You can also possibly convert it to any other type with 
>> implicit conversion.
>
> Not quite. I think you mean "you can also INTENTIONALLY convert 
> it to SELECT other types with implicit conversion".

The implicit conversion can be intentionally. But if you change a 
function signature, and it can implicitly convert to it. When you 
first wrote the code you didn't intend for it to be implicitly 
converted for that function. Code changes, and this sort of 
change won't give a compiler error when you make a change that 
wasn't intended to happen.

>> This would make it even more difficult to try to read
>> and understand what is going on with implicit conversions. It
>> could make it more powerful, and have the code be simpler. But
>> overall it makes maintainability a pain, especially so since it
>> makes the code look simpler, when in reality it is actually far
>> more complicated.
>
> I find the bigger pain when I need to insert detection for 
> specific things into meta-code, because they require a manual 
> conversion step when the things is something that I'd like to 
> convert implicitly.

Instead of inserting `static if` everywhere like you mention, if 
you have a function that does that instead then you don't need to 
do the checks at the calls sites. You just use the function that 
does the checks for you.

Even still, why couldn't you just put a constructor for this 
case? Couldn't you just give U() the constructor to handle 
SpecialCaseThing? Then you wouldn't need the static if. This just 
isn't a very good example of what you intend, using `static if` 
here is your choice but there are alternatives already available 
to avoid it.


   static if (is(T == SpecialCaseThing)
   {
     U x = special_case_conversion(thing);
   }
   else
   {
     U x = thing; // direct or implicit conversion
   }





More information about the Digitalmars-d mailing list