www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I match every instance of a template type (struct)?

reply rempas <rempas tutanota.com> writes:
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
next sibling parent reply ag0aep6g <anonymous example.com> writes:
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
parent reply rempas <rempas tutanota.com> writes:
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
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
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:
 
 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...
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.
Jul 12 2022
prev sibling parent ag0aep6g <anonymous example.com> writes:
On Tuesday, 12 July 2022 at 13:56:11 UTC, rempas wrote:
 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...
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-list
Jul 12 2022
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
parent Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 12 July 2022 at 15:30:03 UTC, Ali Çehreli wrote:
 An alternative:
 https://dlang.org/phobos/std_traits.html#isInstanceOf
This 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
prev sibling parent rempas <rempas tutanota.com> writes:
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