Thread overview
Can't use float array in map
Jun 03, 2011
Andrej Mitrovic
Jun 03, 2011
Andrej Mitrovic
Jun 03, 2011
bearophile
Jun 03, 2011
Ali Çehreli
Jun 03, 2011
Andrej Mitrovic
June 03, 2011
test case:

import std.algorithm;

void main()
{
    float[10] arr;
    arr[] = 0.0;
    map!((ref float sample){ sample = 1.0; })(arr[]);
}

D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(382): Error: nan is not an lvalue

This looks like a bug to me?
June 03, 2011
Offending line in map:
alias typeof(_fun(.ElementType!R.init)) ElementType;

So it tires to pass .init which will be nan for floats.

A quick workaround would be:
    static if (isFloatingPoint!(ElementType!R))
    {
        .ElementType!R x;
        alias typeof(_fun(x)) ElementType;
    }
    else
    {
        alias typeof(_fun(.ElementType!R.init)) ElementType;
    }

Also, my example was just a little flawed (semantically speaking), since I was missing a return statement:

    double[10] arr;
    arr[] = 1.0;
    auto result = map!((ref double sample){ return 1.0; })(arr[]);

This will then work with that change.
June 03, 2011
Ugh don't I love making silly posts like these. ref won't work for any types, not just floats, since it uses the .init property.

My real problem was that I was using assignment in my first example instead of returning a value. Using map with ref doesn't make much sense. So my complaints are void.

On 6/3/11, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> Offending line in map:
> alias typeof(_fun(.ElementType!R.init)) ElementType;
>
> So it tires to pass .init which will be nan for floats.
>
> A quick workaround would be:
>     static if (isFloatingPoint!(ElementType!R))
>     {
>         .ElementType!R x;
>         alias typeof(_fun(x)) ElementType;
>     }
>     else
>     {
>         alias typeof(_fun(.ElementType!R.init)) ElementType;
>     }
>
> Also, my example was just a little flawed (semantically speaking), since I was missing a return statement:
>
>     double[10] arr;
>     arr[] = 1.0;
>     auto result = map!((ref double sample){ return 1.0; })(arr[]);
>
> This will then work with that change.
>
June 03, 2011
Andrej Mitrovic:

> A quick workaround would be:
>     static if (isFloatingPoint!(ElementType!R))
>     {
>         .ElementType!R x;
>         alias typeof(_fun(x)) ElementType;
>     }
>     else
>     {
>         alias typeof(_fun(.ElementType!R.init)) ElementType;
>     }

If you have improvements for Phobos code, then I suggest to write it in Bugzilla (or even create a pull request in git).

Bye,
bearophile
June 03, 2011
On 06/03/2011 10:15 AM, Andrej Mitrovic wrote:
> Offending line in map:
> alias typeof(_fun(.ElementType!R.init)) ElementType;

The fact that the error points at that line should be a bug. That's just an alias, nothing is evaluated at that line.

Ali