Thread overview
"if" statement
Mar 24, 2019
Francesco Mecca
Mar 24, 2019
ag0aep6g
Mar 25, 2019
Benjamin Schaaf
Mar 25, 2019
Michelle Long
March 24, 2019
https://run.dlang.io/is/zRcj59

```
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?
is this behaviour accepted or should the compiler translate the first case in the second?
March 24, 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.
March 25, 2019
On Sunday, 24 March 2019 at 12:45:13 UTC, Francesco Mecca wrote:
> https://run.dlang.io/is/zRcj59
>
> ```
> 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?
> is this behaviour accepted or should the compiler translate the first case in the second?

You can achieve the same thing by just constructing your algebraic type earlier:

  value = n == 2 ? Alg(2) : Alg("string");
March 25, 2019
On Sunday, 24 March 2019 at 12:45:13 UTC, Francesco Mecca wrote:
> https://run.dlang.io/is/zRcj59
>
> ```
> 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?
> is this behaviour accepted or should the compiler translate the first case in the second?

You could make a Choose function:

auto Ch(A,B)(bool c, A a, B b);

Then

value = Ch(n == 2, n, "string");

Not much different than

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

except you don't have to write Alg all the time.

The compiler should translate the first but that requires implicit conversion of any of the types T... to Algebraic!T... . Of course, that should be possible but is it?