Float[] * Real is a Float[]?

kinke noone at nowhere.com
Tue Dec 10 20:47:16 UTC 2019


On Tuesday, 10 December 2019 at 20:35:26 UTC, Ali Çehreli wrote:
> On 12/10/19 11:26 AM, Adam D. Ruppe wrote:
>> On Tuesday, 10 December 2019 at 19:22:01 UTC, Jonathan Levi 
>> wrote:
>>> Why does this not work?
>> 
>> The [] operators work in place. It doesn't create a new array, 
>> just multiplies the existing elements.
>
> Then the a[]*r syntax should not be allowed. We should be 
> forced to write
>
>  a[] *= r;
>
> No?

No, because the statement is not correct:

void main()
{
     float[3] a = [10,2,4];
     real b = 5.5;
     float[3] c = a[] * b;
     assert(a[0] == 10); // fine, `a` is untouched
}

The actual reason can be seen by inspecting the lowered AST 
(-vcg-ast):

float[3] c = arrayOp(c[], a[], cast(float)b);

or alternatively, by inspecting the LLVM IR produced by LDC, or 
the assembly:

pure nothrow @nogc @trusted float[] 
core.internal.arrayop.arrayOp!(float[], float[], float, "*", 
"=").arrayOp(float[], float[], float)

I.e., the array-op is in single precision and the scalar rhs b is 
cast to a float.


More information about the Digitalmars-d mailing list