"if" statement

ag0aep6g anonymous at example.com
Sun Mar 24 13:09:59 UTC 2019


On 24.03.19 13:45, Francesco Mecca wrote:
> ```
> alias Alg = Algebraic!(int, string);
> 
> void main()
> {
>      int n = 2;
>      Alg value;
> 
>      value = n == 2 ? 2 : "string";
> }
> ```
> 
> The original code used SumType but the effect is the same.
> 
> I suppose that I could write the following:
> 
> ```
>      if(n == 2) value = 2;
>      else value = "string";
> ```
> 
> Is there a workaround for this that maintains a similar syntactic 
> structure?

value = n == 2 ? Alg(2) : Alg("string");

> is this behaviour accepted

Yes.

> or should the compiler translate the first 
> case in the second?

No.


More information about the Digitalmars-d-learn mailing list