Thread overview
How can I match every instance of a template type (struct)?
Jul 12, 2022
rempas
Jul 12, 2022
ag0aep6g
Jul 12, 2022
rempas
Jul 12, 2022
H. S. Teoh
Jul 12, 2022
ag0aep6g
Jul 12, 2022
Ali Çehreli
Jul 12, 2022
Salih Dincer
Jul 12, 2022
rempas
July 12, 2022

I want to do something like the following:

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:

  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.

July 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"); }
July 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...
July 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:
> > 
> > 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.
July 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:

>

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

July 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

July 12, 2022

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:

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

July 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 ;)