Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 25, 2016 Static ternary if | ||||
---|---|---|---|---|
| ||||
Is there a static ternary if? (A == B) ? C : D; for compile type that works like static if. |
July 24, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gorge Jingale | On 07/24/2016 07:15 PM, Gorge Jingale wrote:
> Is there a static ternary if?
>
> (A == B) ? C : D;
>
> for compile type that works like static if.
The way to force an expression at compile time is to use it for something that's needed at compile time. For example, you can initialize a manifest constant (enum) with that expression:
void main() {
enum i = (__MODULE__.length % 2) ? 42 : 43;
pragma(msg, i);
}
Instead of enum, you can use 'static const' as well.
Ali
|
July 25, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 25 July 2016 at 05:00:23 UTC, Ali Çehreli wrote: > On 07/24/2016 07:15 PM, Gorge Jingale wrote: >> Is there a static ternary if? >> >> (A == B) ? C : D; >> >> for compile type that works like static if. > > The way to force an expression at compile time is to use it for something that's needed at compile time. For example, you can initialize a manifest constant (enum) with that expression: > > void main() { > enum i = (__MODULE__.length % 2) ? 42 : 43; > pragma(msg, i); > } > > Instead of enum, you can use 'static const' as well. > > Ali It also works for "real" enumerated types. ******************************** enum ver = 0; // version(Windows) ... else .... enum VersionRelative1 { A = ver ? 1 : 2, B = ver ? 3 : 4, } enum VersionRelative2 { A = !ver ? 1 : 2, B = !ver ? 3 : 4, } unittest { static assert(VersionRelative1.A == 2); static assert(VersionRelative2.A == 1); } ******************************** which is quite cool and not widely known. |
July 25, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gorge Jingale | On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:
> Is there a static ternary if?
>
> (A == B) ? C : D;
>
> for compile type that works like static if.
You can pretty easily make your own;
template staticIf(bool cond, alias a, alias b) {
static if (cond) {
alias staticIf = a;
} else {
alias staticIf = b;
};
};
The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
|
July 25, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cauterite | On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote:
> On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:
>> Is there a static ternary if?
>>
>> (A == B) ? C : D;
>>
>> for compile type that works like static if.
>
> You can pretty easily make your own;
>
> template staticIf(bool cond, alias a, alias b) {
> static if (cond) {
> alias staticIf = a;
> } else {
> alias staticIf = b;
> };
> };
>
> The drawback is that there's no 'short-circuiting'; a and b are both evaluated.
Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule.
|
July 26, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gorge Jingale | On Monday, 25 July 2016 at 22:57:05 UTC, Gorge Jingale wrote: > On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote: >> On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote: >>> Is there a static ternary if? >>> >>> (A == B) ? C : D; >>> >>> for compile type that works like static if. >> >> You can pretty easily make your own; >> >> template staticIf(bool cond, alias a, alias b) { >> static if (cond) { >> alias staticIf = a; >> } else { >> alias staticIf = b; >> }; >> }; >> >> The drawback is that there's no 'short-circuiting'; a and b are both evaluated. > > Cool, that would work. I don't think the lazy evaluation is a problem at compile time? Just makes for longer times, but should be pretty minuscule. If that's ok, then try out std.traits.Select or std.traits.select: https://dlang.org/phobos/std_traits.html#Select |
July 26, 2016 Re: Static ternary if | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Coulombe | On Tuesday, 26 July 2016 at 00:54:59 UTC, Michael Coulombe wrote:
> If that's ok, then try out std.traits.Select or std.traits.select:
> https://dlang.org/phobos/std_traits.html#Select
Damn, I looked real hard for that template, I knew it existed. I expected it to be in std.meta though.
|
Copyright © 1999-2021 by the D Language Foundation