Improvement to std.numeric examples in documentation

BoraxMan rotflol2 at hotmail.com
Sat Dec 28 08:11:06 UTC 2019


On Saturday, 28 December 2019 at 03:55:18 UTC, Paul Backus wrote:
> On Saturday, 28 December 2019 at 03:28:33 UTC, BoraxMan wrote:
>> When one tries to do something like "normalize(a,10)", dmd 
>> reports this error.
>>
>>> Error: template instance normalize!(no, 100) does not match 
>>> template declaration normalize(R)(R range, ElementType!R sum 
>>> = 1)
>>
>> One must actually specify the template parameter, thus
>>
>>> assert(normalise!(typeof(a)(a,100));
>
> This looks like an instance of issue 1807 [1]. The fix is to 
> move the alias template (in this case, ElementType) out of the 
> argument list and into a template constraint:
>
> bool normalize(R, S)(R range, S sum = 1)
>     if (isForwardRange!R && is(S == ElementType!R))
>
> You can work around this particular issue in your own code with 
> the following wrapper function:
>
> auto normalize(R, S)(R range, S sum)
>     if (is(S == ElementType!R ))
> {
>     import std.numeric: normalize;
>     return normalize!R(range, sum);
> }
>
> [1] https://issues.dlang.org/show_bug.cgi?id=1807

Thanks for that.  I've created a pull request.  I ended up using 
my own version which was modified to correctly handle arrays of 
integers.  I think this function was specifically created only to 
work with floating points.  Either it should work with integers, 
or have a constraint where it won't accept integers.  Not sure 
what the original intent was.



More information about the Digitalmars-d mailing list