www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin template

reply Vindex <tech.vindex gmail.com> writes:
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);
     }
}
```
May 23 2022
next sibling parent vit <vit vit.vit> writes:
On Monday, 23 May 2022 at 15:14:53 UTC, Vindex wrote:
 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);
     }
 }
 ```
mixin template create scope, for example if it was normal function (no ctor), you can do this: ```d mixin template RealizeException() { static void foo(string){} } class WrongUsage{ mixin RealizeException x; alias foo = x.foo; static void foo(string[]){} } void main() { WrongUsage.foo("Error message."); } ``` But for ctor this doesn't work...
May 23 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/23/22 08:14, Vindex wrote:

 Why? Why can't I have two constructors when I use mixin?
And there is an example in Phobos: https://dlang.org/library/std/exception/basic_exception_ctors.html The documentation there mentions the following bug: https://issues.dlang.org/show_bug.cgi?id=11500 Ali
May 23 2022