food for thought - swift 5 released - bottom types, string interpolation, and stuff.

Timon Gehr timon.gehr at gmx.ch
Mon Apr 15 13:20:04 UTC 2019


On 15.04.19 09:36, Walter Bright wrote:
> On 4/14/2019 5:58 AM, Timon Gehr wrote:
>> It is not possible to require specific values as function arguments in 
>> template constraints.
> 
> I believe it is if you use the "template expression" technique 
> popularized in C++. Even if not,

It's not possible.

> or if you understandably don't care for 
> expression templates (I don't, either), you can overload based on the 
> right operand being an int, and then special case it with a runtime test 
> for '1'.
> 
> That test won't add significantly to the runtime if you've represented 
> the integer as a series of primes and exponents.
> ..
It would be ridiculous though. What do you suggest happens if the 
runtime check fails?

>  > It should return `false` for every input that is not an integer.
> 
> Of course, the compiler will give an error for (f & 1), so the user will 
> have an opportunity to decide what to do about it and if his algorithm 
> makes any sense with odd floating point values, which it likely 
> wouldn't. He then can write his own isOdd() function as necessary. We 
> don't need to do it for him, and are not likely providing a useful 
> service even if we did.

That's basically what I wrote earlier in the same post. I then proceeded 
to answer your question anyway, showing that the behaviour of 
isOdd(infinity) is actually clear. isOdd(infinity), if it compiles at 
all, should return false.

(There may be some miscommunication going on. I believe this has 
happened a few times in the past as well. Sometimes, I explicitly agree 
with your conclusion, but I don't agree with your argumentation. You 
then seem to wrongly assume that I disagree with the conclusion, from 
which you conclude that I am wrong and my arguments do not matter.)


More information about the Digitalmars-d mailing list