mixin template

Vindex tech.vindex at gmail.com
Mon May 23 15:14:53 UTC 2022


I have this code:

```
import std.array, std.exception, std.stdio;

mixin template RealizeException() {
     this(string msg, string file = __FILE__, size_t line = 
__LINE__) {
         super(msg, file, line);
     }
}

class WrongUsage : Exception {
     mixin RealizeException;

     this(string[] messages, string file = __FILE__, size_t line = 
__LINE__) {
         auto msg = std.array.join(messages, "\n");
         super(msg, file, line);
     }
}

void main() {
     throw new WrongUsage("Error message.");
}
```

... and this error:

```
mixin_exception.d(19): Error: constructor 
`mixin_exception.WrongUsage.this(string[] messages, string file = 
__FILE__, ulong line = cast(ulong)__LINE__)` is not callable 
using argument types `(string)`
mixin_exception.d(19):        cannot pass argument `"Error 
message."` of type `string` to parameter `string[] messages`
Failed: ["/usr/bin/dmd", "-v", "-o-", "mixin_exception.d", "-I."]

```

Why? Why can't I have two constructors when I use mixin?

If I replace mixin to real code, I have no problem:

```
class WrongUsage : Exception {
     this(string msg, string file = __FILE__, size_t line = 
__LINE__) {
         super(msg, file, line);
     }

     this(string[] messages, string file = __FILE__, size_t line = 
__LINE__) {
         auto msg = std.array.join(messages, "\n");
         super(msg, file, line);
     }
}
```


More information about the Digitalmars-d-learn mailing list