www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't use float array in map

reply Andrej Mitrovic <none none.none> writes:
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?
Jun 03 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.
Jun 03 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Jun 03 2011
prev sibling parent =?ISO-8859-1?Q?Ali_=C7ehreli?= <acehreli yahoo.com> writes:
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
Jun 03 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.

Jun 03 2011