Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 21, 2014 Scalar + array operations | ||||
---|---|---|---|---|
| ||||
When working on my current project (writing a numerical simulation code) I ran into the following issue when trying to multiply a vector (represented by a fixed-length array) by a scalar: import std.stdio; void main() { int ifoo = 2; int[3] ibar = 1; double dfoo = 2.0; double[3] dbar = 1.0; dfoo = ifoo * dfoo; // Scalar int * scalar double -- OK writeln(dfoo); dfoo = dfoo * dfoo; // Scalar double * scalar double -- OK writeln(dfoo); dbar = dfoo * dbar[]; // Scalar double * array of double -- OK writeln(dbar); ibar = ifoo * ibar[]; // Scalar int * array of int -- OK writeln(ibar); dbar = ifoo * dbar[]; // Scalar int * array of double -- OK writeln(dbar); // dbar = dfoo * ibar[]; // Scalar double * array of int -- FAIL // writeln(dbar); } I would have expected the last case to work as well, but I get testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 'double' and 'int[]' Is this by design? It was very surprising to me, especially since all other combinations do seem to work. Kind regards, Stefan Frijters |
May 21, 2014 Re: Scalar + array operations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Frijters | Stefan Frijters:
> Is this by design? It was very surprising to me, especially since all other combinations do seem to work.
I don't know if this situation is by design. At first sights it seems a limitation that could be removed.
Bye,
bearophile
|
May 21, 2014 Re: Scalar + array operations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Frijters | On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote:
> When working on my current project (writing a numerical simulation code) I ran into the following issue when trying to multiply a vector (represented by a fixed-length array) by a scalar:
>
> import std.stdio;
>
> void main() {
> int ifoo = 2;
> int[3] ibar = 1;
>
> double dfoo = 2.0;
> double[3] dbar = 1.0;
>
> dfoo = ifoo * dfoo; // Scalar int * scalar double -- OK
> writeln(dfoo);
> dfoo = dfoo * dfoo; // Scalar double * scalar double -- OK
> writeln(dfoo);
> dbar = dfoo * dbar[]; // Scalar double * array of double -- OK
> writeln(dbar);
> ibar = ifoo * ibar[]; // Scalar int * array of int -- OK
> writeln(ibar);
> dbar = ifoo * dbar[]; // Scalar int * array of double -- OK
> writeln(dbar);
> // dbar = dfoo * ibar[]; // Scalar double * array of int -- FAIL
> // writeln(dbar);
> }
>
> I would have expected the last case to work as well, but I get
>
> testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 'double' and 'int[]'
>
> Is this by design? It was very surprising to me, especially since all other combinations do seem to work.
>
> Kind regards,
>
> Stefan Frijters
Please file a bug, there's no reason for that not to work, it just needs to be implemented properly.
|
May 21, 2014 Re: Scalar + array operations | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote: > Please file a bug, there's no reason for that not to work, it just needs to be implemented properly. Ok, thanks for confirming. Filed as https://issues.dlang.org/show_bug.cgi?id=12780 . |
May 21, 2014 Re: Scalar + array operations | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote:
> On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote:
>> I would have expected the last case to work as well, but I get
>>
>> testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 'double' and 'int[]'
>>
>> Is this by design? It was very surprising to me, especially since all other combinations do seem to work.
>>
>> Kind regards,
>>
>> Stefan Frijters
>
> Please file a bug, there's no reason for that not to work, it just needs to be implemented properly.
To me, it just feels reasonable that it is not allowed. What should be the correct type of the result? int[]? I thought double to int conversion was not allowed unless you explicitly asked for it.
|
May 21, 2014 Re: Scalar + array operations | ||||
---|---|---|---|---|
| ||||
Posted in reply to Francesco Cattoglio | On Wednesday, 21 May 2014 at 17:07:27 UTC, Francesco Cattoglio wrote:
> On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote:
>> On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote:
>>> I would have expected the last case to work as well, but I get
>>>
>>> testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 'double' and 'int[]'
>>>
>>> Is this by design? It was very surprising to me, especially since all other combinations do seem to work.
>>>
>>> Kind regards,
>>>
>>> Stefan Frijters
>>
>> Please file a bug, there's no reason for that not to work, it just needs to be implemented properly.
>
> To me, it just feels reasonable that it is not allowed. What should be the correct type of the result? int[]? I thought double to int conversion was not allowed unless you explicitly asked for it.
No, I expected and desired an array of doubles, implicitly converting the array of ints to doubles.
|
Copyright © 1999-2021 by the D Language Foundation