Why 1f.iota(100f).array returns double[] not float[]?

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 8 06:23:07 PDT 2015


On Tuesday, 8 September 2015 at 12:28:07 UTC, Dominikus Dittes 
Scherkl wrote:
> On Tuesday, 8 September 2015 at 07:17:01 UTC, Ali Çehreli wrote:
>> https://github.com/D-Programming-Language/phobos/blob/master/std/range/package.d#L4630
>>
>> auto iota(B, E)(B begin, E end)
>> if (isFloatingPoint!(CommonType!(B, E)))
>> {
>>     return iota(begin, end, 1.0);
>> }
>>
> Such kind of stuff would better be written as
>
> auto iota(B, E)(B begin, E end)
> {
>     return iota(begin, end, cast(CommonType!(B, E))1.0);
> }
>
> this doesn't need a constraint anymore, or maybe use
>
> if(isNumeric!(CommonType!(B, E)))
>
> I tend to use the above kind of cast often, because I like to 
> work with ubyte or ushort, and every f***ing number literal 
> changes the type to uint :-/

Even better, use D's universal construction feature.

auto iota(B, E)(B begin, E end)
{
     return iota(begin, end, CommonType!(B, E)(1.0));
}


More information about the Digitalmars-d-learn mailing list