Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn
On Friday, 17 December 2021 at 13:00:55 UTC, Ali Çehreli wrote: On 12/17/21 1:57 AM, bauss wrote: > You can also do it as a normal template: > > ```d > template is_same(alias value, T) > { > enum is_same = is(typeof(value) == T); > } > ``` And even shorter by realizing that it's an

Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn
On Friday, 17 December 2021 at 16:02:45 UTC, RazvanN wrote: There is also a compiler trait [1] which can do that for you: ```d void main() { int val = 10; static if (__traits(isSame, typeof(val), int)) {} } ``` [1] https://dlang.org/spec/traits.html#isSame Thanks! The other options

Re: Is it possible to do this with a template?

2021-12-17 Thread RazvanN via Digitalmars-d-learn
On Friday, 17 December 2021 at 07:52:18 UTC, rempas wrote: I want to use an expression and put it in place inside the `if` parentheses. The expression is: `is(typeof(val) == type)`. I want to use a template called "is_same" that will take the value and a type to place them to the respective

Re: Is it possible to do this with a template?

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/21 1:57 AM, bauss wrote: > You can also do it as a normal template: > > ```d > template is_same(alias value, T) > { > enum is_same = is(typeof(value) == T); > } > ``` And even shorter by realizing that it's an eponymous template: enum is_same(alias value, T) = is(typeof(value) ==

Re: Is it possible to do this with a template?

2021-12-17 Thread bauss via Digitalmars-d-learn
On Friday, 17 December 2021 at 08:59:19 UTC, rempas wrote: On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote: It isn't really about limitation of templates. You're trying to use mixin template and it's main purpose is to inject declarations. If you want to replace `is expression`

Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn
On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote: It isn't really about limitation of templates. You're trying to use mixin template and it's main purpose is to inject declarations. If you want to replace `is expression` with template you could use something like this: ```d bool

Re: Is it possible to do this with a template?

2021-12-17 Thread Mitacha via Digitalmars-d-learn
On Friday, 17 December 2021 at 07:52:18 UTC, rempas wrote: I want to use an expression and put it in place inside the `if` parentheses. The expression is: `is(typeof(val) == type)`. I want to use a template called "is_same" that will take the value and a type to place them to the respective

Is it possible to do this with a template?

2021-12-16 Thread rempas via Digitalmars-d-learn
I want to use an expression and put it in place inside the `if` parentheses. The expression is: `is(typeof(val) == type)`. I want to use a template called "is_same" that will take the value and a type to place them to the respective places. I have tried the following but it doesn't seem to