www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Algebraic and implicit conversions

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is there a reason why

     alias A = Algebraic!(long, string);
     A x;
     x = 42;

doesn't implicitly convert `42` to `long` and stores it in `x`?
Jan 06 2020
parent reply drug <drug2004 bk.ru> writes:
06.01.2020 20:08, Per Nordlöw пишет:
 alias A = Algebraic!(long, string);
      A x;
      x = 42;
That's because you try to assign int, not long. This works: alias A = Algebraic!(long, string); A x; x = 42L;
Jan 06 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/6/20 2:33 PM, drug wrote:
 06.01.2020 20:08, Per Nordlöw пишет:
 alias A = Algebraic!(long, string);
      A x;
      x = 42;
That's because you try to assign int, not long. This works: alias A = Algebraic!(long, string); A x; x = 42L;
I think his question is, why shouldn't this work? e.g., this works: long x; x = 42; FWIW, 3rd-party algebraic types (e.g. TaggedAlgebraic) will do a conversion for this. -Steve
Jan 06 2020
parent drug <drug2004 bk.ru> writes:
06.01.2020 22:48, Steven Schveighoffer пишет:
 
 I think his question is, why shouldn't this work? e.g., this works:
 
Ah, indeed. Sorry for noise.
Jan 06 2020