digitalmars.D.learn - Can't use float array in map
- Andrej Mitrovic (10/10) Jun 03 2011 test case:
- Andrej Mitrovic (19/19) Jun 03 2011 Offending line in map:
- bearophile (4/14) Jun 03 2011 If you have improvements for Phobos code, then I suggest to write it in ...
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (4/6) Jun 03 2011 The fact that the error points at that line should be a bug. That's just...
- Andrej Mitrovic (6/25) Jun 03 2011 Ugh don't I love making silly posts like these. ref won't work for any
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
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
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
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
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









bearophile <bearophileHUGS lycos.com> 