Passing template alias to a function

monkyyy crazymonkyyy at gmail.com
Thu Aug 7 17:33:07 UTC 2025


On Thursday, 7 August 2025 at 16:58:34 UTC, Andrey Zherikov wrote:
> On Wednesday, 6 August 2025 at 23:58:03 UTC, monkyyy wrote:
>> ```d
>> struct A(T) {}
>> struct H(T) {}
>>
>> alias B(T) = A!(H!T);
>>
>> void f(T)(A!(H!T) b) {}
>> void g(T)(A!T b) {}
>> void h(B_)(B_ b)if(is(B_==A!(H!T),T)){}
>> unittest{
>>     f(B!string.init);
>>     g(B!int.init);
>>     h(B!bool.init);
>> }
>> ```
>>
>> I dont think you should actually try to pull patterns out of 
>> `h`, and just go with a `void i(T...)(T args)` if you have 
>> something complex to match but without a usecase, who knows
>
> I don't want to change function parameter to `A!...` because 
> `A!...` is implementation details of `B`:
> ```d
> // module a
> public struct A(T) {}
>
> // module b
> private struct H(T) {}
> public alias B(T) = A!(H!T);
>
> // module main
> void f(T)(B!T b) {}
> ```

I believe your being silly but:

```d
#!opend -unittest -main -run main.d
--- a.d
public struct A(T) {}


--- b.d
import a;
private struct H(T) {}
public alias B(T) = A!(H!T);
public template enforceB(T){
	static assert(is(T==A!(H!S),S));
	enum enforceB=true;
}
public template getBargs(T){
	enum _=is(T==A!(H!S),S);
	alias getBargs=S;
}

--- main.d
import b;
void f(B_)(B_ b)if(enforceB!B_){
	pragma(msg,getBargs!B_.stringof);
}

unittest{
	f(B!string.init);
	f(B!int.init);
}
```


More information about the Digitalmars-d-learn mailing list