digitalmars.D.learn - How can I match every instance of a template type (struct)?
- rempas (37/37) Jul 12 2022 I want to do something like the following:
- ag0aep6g (2/6) Jul 12 2022 static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!!!!!\n"); }
- rempas (3/5) Jul 12 2022 Haaaaaah? Ok, what does this work anyway? I thought you needed
- H. S. Teoh (6/12) Jul 12 2022 If your template has multiple parameters, just write:
- ag0aep6g (5/11) Jul 12 2022 The second `T` is not a template argument. It's an operand of the
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/7) Jul 12 2022 An alternative:
- Salih Dincer (22/24) Jul 12 2022 This is a really good alternative. Because I used to have to
- rempas (5/6) Jul 12 2022 Thank you all for your help!
I want to do something like the following: ```d import core.stdc.stdio; Test!(T, "mode1") make_test(T)(T data) { Test!(T, "mode1") t = { data }; return t; } struct Test(T, string mode = "ref") { T data; } extern (C) void main() { auto obj = make_test(20); static if (is(typeof(obj) == Test)) { printf("YES!!!!!!!\n"); } } ``` So, I just want to be able check if a variable is a given struct type. I also want to be able to do something similar but having a (templated) function that returns a struct type without having to be limited to a specific initialization of it. Obviously, the given code will not work. It will result to the following error message: ``` Error: template struct `test.Test(T, string mode = "ref")` is used as a type without instantiation; to instantiate it use `Test!(arguments)` ``` Any ideas? Also, like in every question I make, the solution must be "betterC" compatible. **CLARIFICATION** I have a bad feeling that the post is not clear enough, so I'll save us all some time by making it clear. I know I can do this: ```d static if (is(typeof(obj) == Test!(int, "mode1"))) { printf("YES!!!!!!!\n"); } ``` And it will work but this is not what I want. I want to much EVERY "Test" type regardless of what's the value of its templated arguments.
Jul 12 2022
On 12.07.22 15:34, rempas wrote:extern (C) void main() { auto obj = make_test(20); static if (is(typeof(obj) == Test)) { printf("YES!!!!!!!\n"); } }static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!!!!!\n"); }
Jul 12 2022
On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote:static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!!!!!\n"); }Haaaaaah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated arguments...
Jul 12 2022
On Tue, Jul 12, 2022 at 01:56:11PM +0000, rempas via Digitalmars-d-learn wrote:On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote:If your template has multiple parameters, just write: static if (is(typeof(obj) == Test!Args, Args...)) ... T -- Don't drink and derive. Alcohol and algebra don't mix.static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!!!!!\n"); }Haaaaaah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated arguments...
Jul 12 2022
On Tuesday, 12 July 2022 at 13:56:11 UTC, rempas wrote:On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote:The second `T` is not a template argument. It's an operand of the "IsExpression". You can read more about those expressions here: https://dlang.org/spec/expression.html#is-parameter-liststatic if (is(typeof(obj) == Test!T, T)) { printf("YES!!!!!!!\n"); }Haaaaaah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated arguments...
Jul 12 2022
On 7/12/22 06:34, rempas wrote:static if (is(typeof(obj) == Test)) { printf("YES!!!!!!!\n"); }An alternative: import std.traits; static if (isInstanceOf!(Test, typeof(obj))) { printf("YES!!!!!!!\n"); } https://dlang.org/phobos/std_traits.html#isInstanceOf Ali
Jul 12 2022
On Tuesday, 12 July 2022 at 15:30:03 UTC, Ali Çehreli wrote:An alternative: https://dlang.org/phobos/std_traits.html#isInstanceOfThis is a really good alternative. Because I used to have to write longer. Thanks, the LOG thing is better now: ```d struct LOG(T...) { T[0] id; T[1] data; } void main() { auto obj = //make_test(20);/* make_test('T');//*/ alias typ = //typeof(obj);/* LOG!(int, char);//*/ "Type: ".write; if(isInstanceOf!(LOG, typ)/* is(typ : Template!Args, alias Template, Args...)//*/ ) "LOG".writeln; } ``` SDB 79
Jul 12 2022
On Tuesday, 12 July 2022 at 13:34:42 UTC, rempas wrote:[...]Thank you all for your help! Ali Çehreli That makes things much much easier! I'll look at the source code in "traits.d" and I'll copy-paste it into my library ;)
Jul 12 2022