Re: Mixin template parameter overloading bug?

2025-06-17 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 15 June 2025 at 16:14:28 UTC, Manfred Nowak wrote: On Sunday, 15 June 2025 at 10:51:37 UTC, Nick Treleaven wrote: An untyped parameter does make the literal an actual template: ```d pragma(msg, __traits(isTemplate, (x) {})); // true ``` It can be instantiated with a type parameter.

Re: Mixin template parameter overloading bug?

2025-06-15 Thread Manfred Nowak via Digitalmars-d-learn
On Sunday, 15 June 2025 at 10:51:37 UTC, Nick Treleaven wrote: An untyped parameter does make the literal an actual template: ```d pragma(msg, __traits(isTemplate, (x) {})); // true ``` It can be instantiated with a type parameter. Therefore in the case discussed, the code `(_){}' declares a

Re: Mixin template parameter overloading bug?

2025-06-15 Thread Monkyyy via Digitalmars-d-learn
On Sunday, 15 June 2025 at 10:51:37 UTC, Nick Treleaven wrote: So I think there must be another reason why your unary literals work. Given it's specifically void return, and top level and it acts like it matches the first return, I expect that void is incorrectly being considered a invalid

Re: Mixin template parameter overloading bug?

2025-06-15 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 14 June 2025 at 23:49:19 UTC, Steven Schveighoffer wrote: A lambda is a shortened syntax for a function literal or delegate literal. HOWEVER, when you do not give the parameters types, the lambda becomes a template! Well, not actually a template, but a quasi-template. An untype

Re: Mixin template parameter overloading bug?

2025-06-14 Thread monkyyy via Digitalmars-d-learn
On Saturday, 14 June 2025 at 23:49:19 UTC, Steven Schveighoffer wrote: If I had to guess, I think the compiler is not able to exactly deduce what form your lambda should be instantiated with. I think it's picking the first form it sees. I think if anything, the error message is really weird.

Re: Mixin template parameter overloading bug?

2025-06-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On Saturday, 14 June 2025 at 00:02:32 UTC, Andrey Zherikov wrote: The simplest code that shows the issue: ```d struct S{} template f(void function(S) F) {} template f(int function(S) F) {} mixin f!((_) {}); mixin f!((_) => 0); // Error: cannot return non-void from `void` function

Re: Mixin template parameter overloading bug?

2025-06-14 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 14 June 2025 at 03:17:09 UTC, monkyyy wrote: ```d import std; mixin template f(int function(int) F){} mixin template f(void function(int) F){unittest{"void".writeln;}} //mixin f!((_){}); //FAILS mixin template g(void function(int) F){unittest{"void&quo

Re: Mixin template parameter overloading bug?

2025-06-13 Thread monkyyy via Digitalmars-d-learn
oid f(void function(int) F)(){"void".writeln;} unittest{ f!((_) {}); f!((_) => 0); } mixin template g(int function(int) F){string s1="int";} mixin template g(void function(int) F){string s2="void";} unittest{ mixin g!((_) {}); mixin

Re: Mixin template parameter overloading bug?

2025-06-13 Thread Andrey Zherikov via Digitalmars-d-learn
f!((_) {}); f!((_) => 0); } mixin template g(int function(int) F){string s1="int";} mixin template g(void function(int) F){string s2="void";} unittest{ mixin g!((_) {}); mixin g!((_) => 0); s1.writeln; s2.writeln; } ``

Re: Mixin template parameter overloading bug?

2025-06-13 Thread Andrey Zherikov via Digitalmars-d-learn
hile the latter can't. In my case mixin template generates top-level `main()` function but the content of the template is not important here. Also using `alias F` as template parameter doesn't allow me to introspect the actual type.

Re: Mixin template parameter overloading bug?

2025-06-13 Thread monkyyy via Digitalmars-d-learn
our still mixing syntax; mixin templates are suppose to be a separate system according to the spec. ```d import std; void f(int function(int) F)(){"int".writeln;} void f(void function(int) F)(){"void".writeln;} unittest{ f!((_) {}); f!((_) => 0); } mixin templa

Re: Mixin template parameter overloading bug?

2025-06-13 Thread Andrey Zherikov via Digitalmars-d-learn
On Saturday, 14 June 2025 at 00:02:32 UTC, Andrey Zherikov wrote: Simplified test case a bit more. This works: ```d template f(void function(int) F) {} template f(int function(int) F) {} mixin f!((int _) {}); mixin f!((int _) => 0); mixin f!((int) {}); mixin f!((int) => 0); mixin f!((_) {}); mi

Re: Mixin template parameter overloading bug?

2025-06-13 Thread monkyyy via Digitalmars-d-learn
On Saturday, 14 June 2025 at 00:02:32 UTC, Andrey Zherikov wrote: The simplest code that shows the issue: ```d struct S{} template f(void function(S) F) {} template f(int function(S) F) {} mixin f!((_) {}); mixin f!((_) => 0); // Error: cannot return non-void from `void` function

Mixin template parameter overloading bug?

2025-06-13 Thread Andrey Zherikov via Digitalmars-d-learn
The simplest code that shows the issue: ```d struct S{} template f(void function(S) F) {} template f(int function(S) F) {} mixin f!((_) {}); mixin f!((_) => 0); // Error: cannot return non-void from `void` function // mixin f!((_) => 0); //

Re: Create module-level function using mixin template?

2025-04-30 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 30 April 2025 at 06:08:23 UTC, cc wrote: On Friday, 25 April 2025 at 16:14:49 UTC, Andy Valencia wrote: I have a code pattern, and would like to generate rather than copy/paste. It _seems_ like mixin templates apply, but I'm not having much luck. I saw one comment that templates

Re: Create module-level function using mixin template?

2025-04-29 Thread cc via Digitalmars-d-learn
On Friday, 25 April 2025 at 16:14:49 UTC, Andy Valencia wrote: I have a code pattern, and would like to generate rather than copy/paste. It _seems_ like mixin templates apply, but I'm not having much luck. I saw one comment that templates always expand in their own context, so perhaps they're

Re: Create module-level function using mixin template?

2025-04-25 Thread monkyyy via Digitalmars-d-learn
On Friday, 25 April 2025 at 17:24:18 UTC, Andy Valencia wrote: On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote: its extremely unclear what your trying to do my best geuss: I want to use a mixin template to generate a top-level function. Like, is there a variant of the following

Re: Create module-level function using mixin template?

2025-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/25 10:24 AM, Andy Valencia wrote: On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote: its extremely unclear what your trying to do my best geuss: I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function

Re: Create module-level function using mixin template?

2025-04-25 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 25, 2025 at 05:24:18PM +, Andy Valencia via Digitalmars-d-learn wrote: > On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote: > > its extremely unclear what your trying to do my best geuss: > > I want to use a mixin template to generate a top-level function.

Re: Create module-level function using mixin template?

2025-04-25 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote: its extremely unclear what your trying to do my best geuss: I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function named "foo1" available? Andy

Re: Create module-level function using mixin template?

2025-04-25 Thread monkyyy via Digitalmars-d-learn
rhaps they're not useful for generating a top-level function? Andy its extremely unclear what your trying to do my best geuss: ```d int func1()=>3; int func2()=>4; mixin template makenewfunction(alias F,int reference){ int newfunction(int i:reference)(){ return F(

Create module-level function using mixin template?

2025-04-25 Thread Andy Valencia via Digitalmars-d-learn
Andy ```d bool bigtest(in string s) { return true; } bool test1(in char c) { return false; } bool test2(in char c) { return true; } mixin template MyFunc(alias fn) { bool fn(in string s) { if (!bigtest(s)) { return false; } foreach(c; s) {

Re: How to specity a list of fields with default to a mixin template?

2025-03-26 Thread realhet via Digitalmars-d-learn
On Sunday, 2 March 2025 at 23:28:09 UTC, Inkrementator wrote: On Sunday, 2 March 2025 at 19:31:06 UTC, realhet wrote: Anyone have an idea? While template mixins have access to the caller scope, the default values for parameters apparently don't. Thank you, both of you! That was the key to

Re: How to specity a list of fields with default to a mixin template?

2025-03-02 Thread Inkrementator via Digitalmars-d-learn
On Sunday, 2 March 2025 at 19:31:06 UTC, realhet wrote: Anyone have an idea? While template mixins have access to the caller scope, the default values for parameters apparently don't. If you `inline` the default value, it will work ``` import std.traits; mixin template T(strin

Re: How to specity a list of fields with default to a mixin template?

2025-03-02 Thread monkyyy via Digitalmars-d-learn
On Sunday, 2 March 2025 at 19:31:06 UTC, realhet wrote: Anyone have an idea? Such things are extremely discouraged and I could suggest maybe 3 hacks But I unable to find a way to parse this declaration in the scope where my types are. ```d --- foo.d import std; public import bar; myint fi

How to specity a list of fields with default to a mixin template?

2025-03-02 Thread realhet via Digitalmars-d-learn
Hello, ```d mixin template T(string def, alias C = typeof(mixin("new class{"~def~"}"))) { enum generatedStr = FieldTypeTuple!C.stringof; } void main() { { mixin T!"int a, b;"; pragma(msg, generatedStr); //works } {

Iterating over mixin template members

2022-07-28 Thread Hipreme via Digitalmars-d-learn
So, I've came to a situation where I must use mixin template for defining members overloads, like: ```d mixin template OverloadGen() { static if(hasMethod!(typeof(this), "add", int, int)) { float add(float x, float y){return x+y;} } static if(hasMethod!(typ

Re: mixin template bug with opBinary?

2022-07-22 Thread Paul Backus via Digitalmars-d-learn
On Friday, 22 July 2022 at 12:33:37 UTC, Anthony Quizon wrote: I get: ``` foo.d(16): Error: mixin `foo.B.opBi!(B, ["+":function (B a, B b) pure nothrow @nogc @safe => a])` does not match template declaration `opBi(A, A function(A, A)[string] f0)` ``` Is this a bug or am I doing something wron

Re: mixin template bug with opBinary?

2022-07-22 Thread Anthony Quizon via Digitalmars-d-learn
On Friday, 22 July 2022 at 12:56:44 UTC, Adam D Ruppe wrote: ``` mixin template opBi( alias f0 ) { static foreach (k, f; f0) { typeof(this) opBinary(string op: k)(typeof(this) r) { return f(this, r); } } } ``` Thanks, this seems to do the trick.

Re: mixin template bug with opBinary?

2022-07-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/22/22 8:33 AM, Anthony Quizon wrote: Hello, I'm trying to create a mixin for quick binary operator overloads by passing in types with a corresponding associative array of strings to functions. However, the code I currently have: ``` module foo; mixin template opBi(     A, A fun

Re: mixin template bug with opBinary?

2022-07-22 Thread Adam D Ruppe via Digitalmars-d-learn
if it is supposed to change. I vaguely recall seeing this before but yeah smells buggy anyway. An alternative you might consider is dropping some of the type and using typeof(this): ``` module foo; mixin template opBi( alias f0 ) { static foreach (k, f; f0) { typeof(this

mixin template bug with opBinary?

2022-07-22 Thread Anthony Quizon via Digitalmars-d-learn
Hello, I'm trying to create a mixin for quick binary operator overloads by passing in types with a corresponding associative array of strings to functions. However, the code I currently have: ``` module foo; mixin template opBi( A, A function(A, A)[string] f0, ) { static forea

Re: mixin template

2022-05-23 Thread Ali Çehreli via Digitalmars-d-learn
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=115

Re: mixin template

2022-05-23 Thread vit via Digitalmars-d-learn
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

mixin template

2022-05-23 Thread Vindex via Digitalmars-d-learn
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

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-20 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 20 May 2022 at 14:54:31 UTC, Christopher Katko wrote: If the declarations are at module scope, `static` has no effect, and CTFE will be used for initialization. It won't use CTFE? Why is there a local module requirement? "module scope" just means at the top level in a module. so n

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-20 Thread Mike Parker via Digitalmars-d-learn
On Friday, 20 May 2022 at 14:54:31 UTC, Christopher Katko wrote: So wait, that means if I have a module with extra stuff like D colors.d auto red = // grey and then in my other file D auto white = grey(1.0); It won't use CTFE? Why is there a local module requirement? I'm

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-20 Thread Christopher Katko via Digitalmars-d-learn
On Friday, 20 May 2022 at 02:30:10 UTC, Mike Parker wrote: On Friday, 20 May 2022 at 00:12:44 UTC, Chris Katko wrote: Yeah that occurred to me as I was falling asleep. Though, do I have to a specify ```D static auto myColor = grey(0.5); ``` to ensure it's done at compile time? It's not the en

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread Mike Parker via Digitalmars-d-learn
On Friday, 20 May 2022 at 00:12:44 UTC, Chris Katko wrote: Yeah that occurred to me as I was falling asleep. Though, do I have to a specify ```D static auto myColor = grey(0.5); ``` to ensure it's done at compile time? It's not the end of the world, but ideally, these are static / hardcoded v

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/19/22 8:29 PM, Steven Schveighoffer wrote: Given a CTFE function it's very easy to wrap for ensuring compile-time usage: ```d enum ctGrey(float f) = grey(f); auto myColor = ctGrey!(0.5); ``` That being said, if it's calculatable at compile time, chances are the compiler is already goi

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/19/22 8:12 PM, Chris Katko wrote: On Thursday, 19 May 2022 at 10:35:30 UTC, ag0aep6g wrote: On 19.05.22 12:15, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red   = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue  = C

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread Chris Katko via Digitalmars-d-learn
On Thursday, 19 May 2022 at 10:35:30 UTC, ag0aep6g wrote: On 19.05.22 12:15, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red   = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue  = COLOR(0,0,1,1); auto white = COLOR(1,1,1,

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.05.22 12:15, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red   = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue  = COLOR(0,0,1,1); auto white = COLOR(1,1,1,1); //etc ``` is there a way to do: ```D auto myColor = GR

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread bauss via Digitalmars-d-learn
On Thursday, 19 May 2022 at 10:18:38 UTC, user1234 wrote: On Thursday, 19 May 2022 at 10:15:32 UTC, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue = COLOR(0,0,1,1); au

Re: template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread user1234 via Digitalmars-d-learn
On Thursday, 19 May 2022 at 10:15:32 UTC, Chris Katko wrote: given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue = COLOR(0,0,1,1); auto white = COLOR(1,1,1,1); //etc ``` is there a way to do:

template? mixin? template mixins? for modifying a struct setup

2022-05-19 Thread Chris Katko via Digitalmars-d-learn
given ```D struct COLOR { float r, g, b, a; // a is alpha (opposite of transparency) } auto red = COLOR(1,0,0,1); auto green = COLOR(0,1,0,1); auto blue = COLOR(0,0,1,1); auto white = COLOR(1,1,1,1); //etc ``` is there a way to do: ```D auto myColor = GREY!(0.5); // where GREY!(0.5) becomes C

Re: Mixin template overloads not working

2021-12-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/7/21 1:03 PM, Q. Schroll wrote: On Tuesday, 7 December 2021 at 12:43:40 UTC, Rumbu wrote: Bug or feature? Feature. It even has a name: "overload set". It keeps you from accidentally calling a function you had no idea existed, for example because of a name clash. Not in this case. See

Re: Mixin template overloads not working

2021-12-07 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 7 December 2021 at 12:43:40 UTC, Rumbu wrote: Bug or feature? Feature. It even has a name: "overload set". It keeps you from accidentally calling a function you had no idea existed, for example because of a name clash. Is there any workaround? Yes, the error message is very c

Re: Mixin template overloads not working

2021-12-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/7/21 7:43 AM, Rumbu wrote: On Friday, 3 December 2021 at 10:57:34 UTC, Stanislav Blinov wrote: On Friday, 3 December 2021 at 10:42:37 UTC, Rumbu wrote: Bug or feature? Is there any workaround? The error message explains what to do :) Error: class `mixinover.AnotherVisitor` use of `mi

Re: Mixin template overloads not working

2021-12-07 Thread Rumbu via Digitalmars-d-learn
On Friday, 3 December 2021 at 10:57:34 UTC, Stanislav Blinov wrote: On Friday, 3 December 2021 at 10:42:37 UTC, Rumbu wrote: Bug or feature? Is there any workaround? The error message explains what to do :) Error: class `mixinover.AnotherVisitor` use of `mixinover.Visitor.visit(S s)` is hid

Re: Mixin template overloads not working

2021-12-03 Thread Stanislav Blinov via Digitalmars-d-learn
On Friday, 3 December 2021 at 10:42:37 UTC, Rumbu wrote: Bug or feature? Is there any workaround? The error message explains what to do :) Error: class `mixinover.AnotherVisitor` use of `mixinover.Visitor.visit(S s)` is hidden by `AnotherVisitor`; use `alias visit = Visitor.visit;` to intro

Mixin template overloads not working

2021-12-03 Thread Rumbu via Digitalmars-d-learn
```d class S {} class A:S {} class B:S {} mixin template vmix(T) {    void visit(T t) {} } class Visitor {    void visit(S s) {}    mixin vmix!A;    mixin vmix!B; } class AnotherVisitor: Visitor {    override void visit(A a) {} } ``` This will result in error when I try to override mixin

Re: How to translate this C macro to D mixin/template mixin?

2021-06-16 Thread Tejas via Digitalmars-d-learn
On Wednesday, 16 June 2021 at 05:48:21 UTC, VitaliiY wrote: On Tuesday, 15 June 2021 at 12:39:40 UTC, Dennis wrote: On Tuesday, 15 June 2021 at 12:18:26 UTC, VitaliiY wrote: [...] ```D enum string ADDBITS(string a, string b) = ` { bitbuffer = (bitbuffer<<(`~a~`))|((`~b~`)&((1<<`~a~`)-1));

Re: How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread VitaliiY via Digitalmars-d-learn
On Tuesday, 15 June 2021 at 12:39:40 UTC, Dennis wrote: On Tuesday, 15 June 2021 at 12:18:26 UTC, VitaliiY wrote: [...] ```D enum string ADDBITS(string a, string b) = ` { bitbuffer = (bitbuffer<<(`~a~`))|((`~b~`)&((1<<`~a~`)-1)); numbits += (`~a~`); mixin(STOREBITS); }`; // on use:

Re: How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread VitaliiY via Digitalmars-d-learn
On Tuesday, 15 June 2021 at 12:38:15 UTC, Ali Çehreli wrote: On 6/15/21 5:18 AM, VitaliiY wrote: > STOREBITS and ADDBITS use variables defined in STARTDATA If possible in your use case, I would put those variables in a struct type and make add() a member function. However, a similar type alre

Re: How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread Dennis via Digitalmars-d-learn
On Tuesday, 15 June 2021 at 12:18:26 UTC, VitaliiY wrote: It's simple with STARTDATA as mixin, but STOREBITS and ADDBITS use variables defined in STARTDATA scope, so I can't understand how to do mixin template with it. If the code duplication isn't too bad, consider just e

Re: How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 6/15/21 5:18 AM, VitaliiY wrote: > STOREBITS and ADDBITS use variables defined in STARTDATA If possible in your use case, I would put those variables in a struct type and make add() a member function. However, a similar type already exists as std.bitmanip.BitArray. Ali

How to translate this C macro to D mixin/template mixin?

2021-06-15 Thread VitaliiY via Digitalmars-d-learn
Could anybody help with translation of this C macro to D mixin/mixin template? Here a - unsigned char, b - int. It's simple with STARTDATA as mixin, but STOREBITS and ADDBITS use variables defined in STARTDATA scope, so I can't understand how to do mixin template with it.

Re: mixin template compile-time compute declared name

2020-06-27 Thread NonNull via Digitalmars-d-learn
On Saturday, 27 June 2020 at 21:23:10 UTC, Adam D. Ruppe wrote: On Saturday, 27 June 2020 at 21:10:59 UTC, NonNull wrote: Is it possible to use a template to declare something whose name is computed at compile time? You'd have to string mixin the contents inside the mixin template. W

Re: mixin template compile-time compute declared name

2020-06-27 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 27 June 2020 at 21:10:59 UTC, NonNull wrote: Is it possible to use a template to declare something whose name is computed at compile time? You'd have to string mixin the contents inside the mixin template.

mixin template compile-time compute declared name

2020-06-27 Thread NonNull via Digitalmars-d-learn
Want mixin mytemplate!("foo", .); to be able to declare names dependent upon the text foo in the context it is used. For example declaring enum x_foo = ; blah foo_value = ; . . . . Is it po

Re: Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn
On Thursday, 10 October 2019 at 15:56:36 UTC, Just Dave wrote: I'm trying to get my head around mixing templates. I'm using it as kind of a replacement for class inheritance as it seems to fit better composition over inheritance. So I do something like: mixin template Numb

Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn
I'm trying to get my head around mixing templates. I'm using it as kind of a replacement for class inheritance as it seems to fit better composition over inheritance. So I do something like: mixin template NumberTemplate() { private: int number = 0; public:

Re: Inconsistent behavior of __FILE__ within mixin template

2019-05-29 Thread Andre Pany via Digitalmars-d-learn
d then you can add it as part of the template. mixin template UnitTest(string filename = __FILE__) { private static this() { testClasses ~= TestClass(this.classinfo.name, filename ); } } Thanks a lot. That looks great. Kind regards Andre

Re: Inconsistent behavior of __FILE__ within mixin template

2019-05-29 Thread Exil via Digitalmars-d-learn
On Wednesday, 29 May 2019 at 08:45:45 UTC, Andre Pany wrote: Hi, I have a module a.d --- struct TestClass { string name; string fileName; } TestClass[] testClasses; mixin template UnitTest() { private static string getFileName(string fileName = __FILE__

Inconsistent behavior of __FILE__ within mixin template

2019-05-29 Thread Andre Pany via Digitalmars-d-learn
Hi, I have a module a.d --- struct TestClass { string name; string fileName; } TestClass[] testClasses; mixin template UnitTest() { private static string getFileName(string fileName = __FILE__) { return fileName; } private static this

Re: Calling function explicitly from mixin template results in recursive call instead

2018-12-10 Thread Dennis via Digitalmars-d-learn
On Monday, 10 December 2018 at 21:16:23 UTC, aliak wrote: Does this fix your issue? struct S { mixin operators ops; S opBinary(string op, T)(T a) { alias opBinary = ops.opBinary; // explicitly alias opBinary in this scope return opBinary!op(a); } } It does, thanks

Re: Calling function explicitly from mixin template results in recursive call instead

2018-12-10 Thread aliak via Digitalmars-d-learn
On Sunday, 9 December 2018 at 18:36:50 UTC, Dennis wrote: I'm using Adam's workaround from https://issues.dlang.org/show_bug.cgi?id=19365, but now I have endless recursion. Reduced code: ``` mixin template operators() { S opBinary(string op: "+")(S rhs)

Re: Calling function explicitly from mixin template results in recursive call instead

2018-12-10 Thread Dennis via Digitalmars-d-learn
On Sunday, 9 December 2018 at 18:36:50 UTC, Dennis wrote: Does anyone know how to get this working? I added this to the mixin template: ``` alias mixinOpBinary = opBinary; ``` And called mixinOpBinary instead in the forwarded function. I think that solved it. I think I'll just file an

Calling function explicitly from mixin template results in recursive call instead

2018-12-09 Thread Dennis via Digitalmars-d-learn
I'm using Adam's workaround from https://issues.dlang.org/show_bug.cgi?id=19365, but now I have endless recursion. Reduced code: ``` mixin template operators() { S opBinary(string op: "+")(S rhs) { return rhs; } // (A) auto opBinary(string op,

Re: Can you get typeof(this) in a mixin template - trying to mimic a Kotlin feature here.

2018-08-28 Thread aliak via Digitalmars-d-learn
On Tuesday, 28 August 2018 at 20:58:23 UTC, Alex wrote: Isn't the problem, that inside a template a declaration is expected, and not a foreach? Boh, you're right. I guess I misunderstood mixin templates. Found a way with a normap function though :p void mapSelf(alias self, alias aa)() {

Re: Can you get typeof(this) in a mixin template - trying to mimic a Kotlin feature here.

2018-08-28 Thread Alex via Digitalmars-d-learn
y map } So I'm trying to do something similar in D: mixin template MapMembers(alias aa) { foreach (name; typeof(this).tupleof) { // if name is in aa, then mixin(m = aa[" ~ m ~ "]) ... ish } } struct User { this(Variant[string] aa) { mixin MapMembers!aa; } } Seems I

Can you get typeof(this) in a mixin template - trying to mimic a Kotlin feature here.

2018-08-28 Thread aliak via Digitalmars-d-learn
Hi, I'm trying to do something similar to what Kotlin allows with assigning to member variables from a map. The syntax is very readable and looks like: class User(val map: Map) { val name: String by map val age: Int by map } So I'm trying to do something similar in

Re: why mixin template can not inclulde statement;

2018-08-10 Thread Kagamin via Digitalmars-d-learn
try this: mixin template test(A...){ __gshared int a = A[0]; int dummy = (){ a++; return 0; }(); } import core.stdc.stdio; int main(){ mixin test!1; printf("a=%d\n", a); return 0; }

Re: why mixin template can not inclulde statement;

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 13:17:13 UTC, learnfirst1 wrote: this work, it report no error but give a link problem. (this could be a bug ?) mixin template test(A...){ __gshared int a = A[0]; pragma(inline, true) // remove this will work static extern(C) int test

Re: why mixin template can not inclulde statement;

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 13:10:57 UTC, Kagamin wrote: On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote: Looks like some problem with tuple, try __gshared a = A[0]; this work, it report no error but give a link problem. (this could be a bug ?)

Re: why mixin template can not inclulde statement;

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 13:05:24 UTC, Kagamin wrote: Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place. mixin template test(A...){ __gshared a = A; int dummy = (){ a++; return 0; }(); } extern(C) void main

Re: why mixin template can not inclulde statement;

2018-08-10 Thread Radu via Digitalmars-d-learn
On Friday, 10 August 2018 at 13:05:24 UTC, Kagamin wrote: Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place. mixin template test(A...){ __gshared a = A; int dummy = (){ a++; return 0; }(); } extern(C) void main

Re: why mixin template can not inclulde statement;

2018-08-10 Thread Kagamin via Digitalmars-d-learn
On Friday, 10 August 2018 at 13:01:21 UTC, learnfirst1 wrote: duplicate symbol __D4test4mainUZ8__mixin111__a_field_0i in: test.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Error: linker exited with status 1

Re: why mixin template can not inclulde statement;

2018-08-10 Thread Kagamin via Digitalmars-d-learn
Mixim template can only introduce declarations, not statements, a workaround is a lambda called in place. mixin template test(A...){ __gshared a = A; int dummy = (){ a++; return 0; }(); } extern(C) void main(){ mixin test!123; }

Re: why mixin template can not inclulde statement;

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 12:38:55 UTC, learnfirst1 wrote: mixin template test(A...){ mixin template test(A...){ __gshared a = A; } extern(C) void main(){ mixin test!123; } --- duplicate symbol __D4test4mainUZ8__mixin111__a_field_0i in: test.o ld: 1

why mixin template can not inclulde statement;

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
mixin template test(A...){ __gshared a = A; a++; } extern(C) void main(){ mixin test!123; } - I dont want to use string mixin to intro a lot un want symbol, try with mixin template find this not work. I know if mixin test on global it should not working

Re: is this a bug ? mixin template static function missing!

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 12:05:52 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 11:17:10 UTC, learnfirst1 wrote: If you try the same without the mixin template, you'll see that it doesn't work: struct Test { extern(C) pragma(crt_constructor) static void init(

Re: is this a bug ? mixin template static function missing!

2018-08-10 Thread Simen Kjærås via Digitalmars-d-learn
(){} } void main(){ mixin G!(); // Line 5 init(); } If you try the same without the mixin template, you'll see that it doesn't work: void main() { static extern(C) pragma(crt_constructor) void init(); init(); } Depending on the order of static, extern(C) a

Re: is this a bug ? mixin template static function missing!

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 10:24:55 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: Filed a bug: https://issues.dlang.org/show_bug.cgi?id=19153 template G(){ pragma(crt_constructor) static extern(C) void init(){} } void main(){ mixin G!(); // Li

Re: is this a bug ? mixin template static function missing!

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
On Friday, 10 August 2018 at 10:24:55 UTC, Simen Kjærås wrote: On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: The correct behavior would be for the compiler to show the latter error message for a mixin'd function as well. Filed a bug: https://issues.dlang.org/show_bug.cgi?id=191

Re: is this a bug ? mixin template static function missing!

2018-08-10 Thread Simen Kjærås via Digitalmars-d-learn
On Friday, 10 August 2018 at 08:31:21 UTC, learnfirst1 wrote: #!/usr/bin/env rdmd import core.stdc.stdio; template G(size_t line = __LINE__, A...){ int i = 3; static extern(C) pragma(crt_constructor) void init2(){ printf("init: %d\n", line); } } pragma(c

is this a bug ? mixin template static function missing!

2018-08-10 Thread learnfirst1 via Digitalmars-d-learn
#!/usr/bin/env rdmd import core.stdc.stdio; template G(size_t line = __LINE__, A...){ int i = 3; static extern(C) pragma(crt_constructor) void init2(){ printf("init: %d\n", line); } } pragma(crt_constructor) extern(C) void init1(){ printf("init fro

Re: is this even possible? newbie + mixin template + foreach (allMembers)

2018-04-03 Thread Seb via Digitalmars-d-learn
On Tuesday, 3 April 2018 at 18:49:00 UTC, Carlos Navarro wrote: QUESTION: Obviously I'm no geting mixins/templates nor traits and I'm failing miserably to find/identify the right examples or documentation to help me tackle this thing. What is wrong in this code? is this pattern sintactically

Re: is this even possible? newbie + mixin template + foreach (allMembers)

2018-04-03 Thread Alex via Digitalmars-d-learn
mixin template. A mixin template is basically expected to be mixin-able in global scope or in a class/struct so it can't have any things like function calls because that would be the same as writing `foo();` at global level instead of in the main function. static foreach on the other h

Re: is this even possible? newbie + mixin template + foreach (allMembers)

2018-04-03 Thread WebFreak001 via Digitalmars-d-learn
tactically possible? what I'm getting wrong? CONTEXT: I'm a newbie trying to extend a struct using a mixin template like this one: struct World { floatrotation; bool active; mixin BuildStuff; } mixin template BuildStuff() { foreach(elem;

is this even possible? newbie + mixin template + foreach (allMembers)

2018-04-03 Thread Carlos Navarro via Digitalmars-d-learn
T: I'm a newbie trying to extend a struct using a mixin template like this one: struct World { floatrotation; bool active; mixin BuildStuff; } mixin template BuildStuff() { foreach(elem; __traits(allMembers, typeof(this))) {

Re: Voldemort type for mixin template.

2018-01-11 Thread ChangLong via Digitalmars-d-learn
On Thursday, 11 January 2018 at 21:30:43 UTC, aliak wrote: On Thursday, 11 January 2018 at 08:56:11 UTC, ChangLong wrote: When I try add some sub type for struct with mixin template, seems there is no way to hidden the private type. Is there a way to hidden type from mix template like

Re: Voldemort type for mixin template.

2018-01-11 Thread aliak via Digitalmars-d-learn
On Thursday, 11 January 2018 at 08:56:11 UTC, ChangLong wrote: When I try add some sub type for struct with mixin template, seems there is no way to hidden the private type. Is there a way to hidden type from mix template like Voldemort type ? fake code: mix template TypeX () { alias

Voldemort type for mixin template.

2018-01-11 Thread ChangLong via Digitalmars-d-learn
When I try add some sub type for struct with mixin template, seems there is no way to hidden the private type. Is there a way to hidden type from mix template like Voldemort type ? fake code: mix template TypeX () { alias This = typeof(this); static struct Unique { This

Re: Variadic Mixin/Template classes?

2017-11-25 Thread Chirs Forest via Digitalmars-d-learn
On Saturday, 25 November 2017 at 10:08:36 UTC, vit wrote: On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote: [...] import std.meta : staticMap; class Bar(T) { T bar; } class Foo(Ts...){ staticMap!(Bar, Ts) bars; this(){ static foreach(i, alias T; Ts) bars

Re: Variadic Mixin/Template classes?

2017-11-25 Thread vit via Digitalmars-d-learn
On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote: I'd like to make a class that takes multiple template types (1 - several) which can hold an array/tuple of a second class that are instantiated with those types. class Bar(T) { T bar; } class Foo(T[]){ // not sure how to t

Variadic Mixin/Template classes?

2017-11-25 Thread Chirs Forest via Digitalmars-d-learn
I'd like to make a class that takes multiple template types (1 - several) which can hold an array/tuple of a second class that are instantiated with those types. class Bar(T) { T bar; } class Foo(T[]){ // not sure how to take variadic types here? Bar!(?)[] bars; //not sure how I'd defi

Re: passing member.member alias to mixin template

2017-09-03 Thread Eric_DD via Digitalmars-d-learn
Clear explanation, thanks! I think it would avoid a lot of confusion to disallow the alias f = c1.field notation and only allow the alias f = C.field notation. If necessary one could use alias f = typeof(c1).field

Re: passing member.member alias to mixin template

2017-09-03 Thread ag0aep6g via Digitalmars-d-learn
On 09/03/2017 08:54 PM, Eric_DD wrote: *** This works: struct Array { void foo() { writeln("foo"); } } mixin template arrayOperations(arrays...) { void foo() { foreach(ref a; arrays) a.foo(); } } class Thing { Array data1; Array data2;

  1   2   3   >