that is bug?

Seb seb at wilzba.ch
Sat Apr 7 11:38:07 UTC 2018


On Saturday, 7 April 2018 at 11:19:44 UTC, meppl wrote:
> On Saturday, 7 April 2018 at 10:25:19 UTC, bauss wrote:
>> On Saturday, 7 April 2018 at 09:07:48 UTC, sdvcn wrote:
>>>
>>>         true?stt="AA":stt="BB";    <<<<-----///Out:BB
>>
>> It's an UB.
>>
>> Not a bug.
>
>
> I want `condition ? expr1 : expr2` to behave like:
>
> -----
> auto qc( alias condition, string expr1, string expr2)() {
> 	if( condition) {
> 		return mixin( expr1);
> 	} else {
> 		return mixin( expr2);
> 	}
> }
> -----

You can do so today with lazy:

---
import std.stdio;

auto qc(E1, E2)(bool condition, lazy E1 expr1, lazy E2 expr2)
{
     if (condition)
     {
         return expr1;
     }
     else
     {
         return expr2;
     }
}

void main()
{
     qc(true, "A".writeln, "B".writeln);
     string s;
     qc(true, s = "A", s = "B");
     s.writeln;
}
---

https://run.dlang.io/is/ymZBht
https://dlang.org/articles/lazy-evaluation.html


More information about the Digitalmars-d mailing list