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

Walter Bright newshound2 at digitalmars.com
Mon Apr 15 07:36:00 UTC 2019


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, 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 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.


More information about the Digitalmars-d mailing list