Template with default parameter

bauss jj_1337 at live.dk
Fri Mar 11 07:06:15 UTC 2022


On Friday, 11 March 2022 at 04:41:40 UTC, Andrey Zherikov wrote:
> I have simple template:
> ```d
> template T(int i=3)
> {
>     mixin template M(int m)
>     {
> 	    enum t = i;
>     }
> }
>
> {
> 	mixin T!1.M!1;
> 	pragma(msg, t);   // 1
> }
> {
> 	mixin T!().M!1;
> 	pragma(msg, t);   // 3
> }
> {
> 	mixin T.M!1;      // Error: identifier `M` of `T.M` is not 
> defined
> 					  // Error: mixin `M!1` is not defined
> 	pragma(msg, t);   // Error: undefined identifier `t`
> 					  //        while evaluating `pragma(msg, t)`
> }
> ```
>
> What should I do to be able to write `T.M!...`? I want to omit 
> verbose `!()` for `T`. Note that mixins are essential here.

Create an alias for T!() is the best you can do.

Ex.

```
alias t = T!();
```

There isn't really any better method as far as I know.


More information about the Digitalmars-d-learn mailing list