digitalmars.D.learn - Is it possible to do this with a template?
- rempas (20/20) Dec 16 2021 I want to use an expression and put it in place inside the `if`
- Mitacha (20/40) Dec 17 2021 It isn't really about limitation of templates. You're trying to
- rempas (5/25) Dec 17 2021 Oh! That's nice! I didn't even knew it was possible to create
- bauss (8/36) Dec 17 2021 You can also do it as a normal template:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/11) Dec 17 2021 And even shorter by realizing that it's an eponymous template:
- rempas (2/14) Dec 17 2021 Thanks guys! Yeah, I prefer Ali's shorter syntax!
- RazvanN (10/30) Dec 17 2021 There is also a compiler trait [1] which can do that for you:
- rempas (2/11) Dec 17 2021 Thanks! The other options were simpler and faster to type tho
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 work: ``` mixin template is_same(val, type) { is(typeof(val) == type) } void main() { int val = 10; static if (is_same!(val, int)) {} } ``` When trying to compile, I'm taking the following error message: ``` Error: declaration expected, not `is` ``` Is this a limitation of templates in D or is there a way to bypass this?
Dec 16 2021
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 places. I have tried the following but it doesn't seem to work: ``` mixin template is_same(val, type) { is(typeof(val) == type) } void main() { int val = 10; static if (is_same!(val, int)) {} } ``` When trying to compile, I'm taking the following error message: ``` Error: declaration expected, not `is` ``` Is this a limitation of templates in D or is there a way to bypass this?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 is_same(alias value, T)() { return is(typeof(value) == T); } void main() { int value = 10; static if (is_same!(value, int)) { writeln("it is true!"); } else { writeln("it is false!"); } } ``` Personally, I don't see any benefit with replacing that kind of `is expressions` with templates. Perhaps I'm missing something :)
Dec 17 2021
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 is_same(alias value, T)() { return is(typeof(value) == T); } void main() { int value = 10; static if (is_same!(value, int)) { writeln("it is true!"); } else { writeln("it is false!"); } } ```Oh! That's nice! I didn't even knew it was possible to create template functions like this! Thanks!Personally, I don't see any benefit with replacing that kind of `is expressions` with templates. Perhaps I'm missing something :)The benefits are typing less code and make it more readable and easy on the eyes ;)
Dec 17 2021
On Friday, 17 December 2021 at 08:59:19 UTC, rempas wrote:On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote:You can also do it as a normal template: ```d template is_same(alias value, T) { enum is_same = is(typeof(value) == T); } ```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 is_same(alias value, T)() { return is(typeof(value) == T); } void main() { int value = 10; static if (is_same!(value, int)) { writeln("it is true!"); } else { writeln("it is false!"); } } ```Oh! That's nice! I didn't even knew it was possible to create template functions like this! Thanks!Personally, I don't see any benefit with replacing that kind of `is expressions` with templates. Perhaps I'm missing something :)The benefits are typing less code and make it more readable and easy on the eyes ;)
Dec 17 2021
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) == T); Ali
Dec 17 2021
On Friday, 17 December 2021 at 13:00:55 UTC, Ali Çehreli wrote:On 12/17/21 1:57 AM, bauss wrote:Thanks guys! Yeah, I prefer Ali's shorter syntax!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) == T); Ali
Dec 17 2021
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 places. I have tried the following but it doesn't seem to work: ``` mixin template is_same(val, type) { is(typeof(val) == type) } void main() { int val = 10; static if (is_same!(val, int)) {} } ``` When trying to compile, I'm taking the following error message: ``` Error: declaration expected, not `is` ``` Is this a limitation of templates in D or is there a way to bypass this?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
Dec 17 2021
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#isSameThanks! The other options were simpler and faster to type tho
Dec 17 2021