digitalmars.D.learn - mixin template
- Vindex (46/46) May 23 2022 I have this code:
- vit (17/64) May 23 2022 mixin template create scope, for example if it was normal
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/7) May 23 2022 And there is an example in Phobos:
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
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
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