Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 06, 2015 Static if to compare two types are the exact same | ||||
---|---|---|---|---|
| ||||
What's the best way to do this? I'm assuming this should be best practice: http://dlang.org/traits.html#isSame struct S { } writeln(__traits(isSame, S, S)); |
April 06, 2015 Re: Static if to compare two types are the exact same | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On 4/6/15, Jonathan via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > What's the best way to do this? I'm assuming this should be best > practice: > http://dlang.org/traits.html#isSame > > struct S { } > writeln(__traits(isSame, S, S)); > I'm not even sure when or why this trait was introduced, but you could use a simple is() expression for this, e.g.: static if (is(T == S)) { ... } |
April 06, 2015 Re: Static if to compare two types are the exact same | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Attachments: | On Mon, 06 Apr 2015 19:16:33 +0000, Jonathan wrote:
> What's the best way to do this? I'm assuming this should be best
> practice:
> http://dlang.org/traits.html#isSame
>
> struct S { }
> writeln(__traits(isSame, S, S));
struct S {}
auto s0 = S();
auto s1 = S();
static if (is(typeof(s0) == typeof(s1))) {
pragma(msg, "Woe to the Republic.");
}
|
April 07, 2015 Re: Static if to compare two types are the exact same | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | >> static if (is(T == V))
Are static ifs always checked outside of runtime? Is it possible for a static if condition to be undeterminable outside of runtime, or would such a condition throw a compiler error?
|
April 07, 2015 Re: Static if to compare two types are the exact same | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On Tuesday, 7 April 2015 at 06:37:50 UTC, Jonathan wrote:
>>> static if (is(T == V))
>
> Are static ifs always checked outside of runtime? Is it possible for a static if condition to be undeterminable outside of runtime, or would such a condition throw a compiler error?
'static if' is always run at compile time, so it needs access to compile time information. Fortunately, you can access quite a lot at compile time in D.
|
Copyright © 1999-2021 by the D Language Foundation