Re: Getting all struct members and values with introspection avoiding string mixins

2023-10-05 Thread user1234 via Digitalmars-d-learn
On Thursday, 5 October 2023 at 22:24:06 UTC, mw wrote: On Thursday, 5 October 2023 at 21:41:38 UTC, cc wrote: If you have `T info`, T.tupleof[n] will always match up with info.tupleof[n]. You can think of `info.tupleof[n]` as being rewritten by the compiler in-place as info.whateverFieldTha

Re: Getting all struct members and values with introspection avoiding string mixins

2023-10-05 Thread mw via Digitalmars-d-learn
On Thursday, 5 October 2023 at 21:41:38 UTC, cc wrote: If you have `T info`, T.tupleof[n] will always match up with info.tupleof[n]. You can think of `info.tupleof[n]` as being rewritten by the compiler in-place as info.whateverFieldThatIs. You might try this version (note the double {{ }}

Re: Getting all struct members and values with introspection avoiding string mixins

2023-10-05 Thread cc via Digitalmars-d-learn
ame, mixin( "info." ~ attribName )); } } Is there is some other way to evaluate info.attribName without using string mixins? Cheers, PP If you have `T info`, T.tupleof[n] will always match up with info.tupleof[n]. You can think of `info.tupleof[n]` as being rewritten by the

Re: Getting all struct members and values with introspection avoiding string mixins

2023-09-28 Thread mw via Digitalmars-d-learn
On Sunday, 1 May 2016 at 10:13:47 UTC, H. S. Teoh wrote: Using typeof(T.tupleof) seems a bit circuitous. Here's how I'd do it: void printStructInfo( T )( T info ) { import std.stdio : writefln; foreach (memb; __traits(allMembers, T)) {

Re: A potential use-case for template mixins?

2023-02-06 Thread Hipreme via Digitalmars-d-learn
situation to use template mixins instead of potentially having to include a lot of information in the base class? Or should I go with the traditional "everything is derived from the base class"? I understand if this is light on details, I'll try and provide more if necessary, but

A potential use-case for template mixins?

2023-02-06 Thread Matt via Digitalmars-d-learn
I am (still) writing a 3D graphics engine, and was considering my choices for a scene graph. Originally, I was going to use classes derived from my base SceneNode class for the nodes of the graph, but would this instead be a better situation to use template mixins instead of potentially

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: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 18:58:39 UTC, bachmeier wrote: You can see the ["String mixins" section here](http://ddili.org/ders/d.en/mixin.html) for more details. Mixins are generated at compile time, so if you're referring to a string mixin inside a runtime loop, the c

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread bachmeier via Digitalmars-d-learn
ou can see the ["String mixins" section here](http://ddili.org/ders/d.en/mixin.html) for more details. Mixins are generated at compile time, so if you're referring to a string mixin inside a runtime loop, the code will not be generated every time the loop runs.

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 18:12:35 UTC, Stanislav Blinov wrote: https://dlang.org/spec/traits.html#identifier Thanks!!! Finally I was able to do it! The code is the following (well not in my final project but it's a demonstration): ``` enum add_char(string c) = `if (stdout_index < ST

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 18:06:32 UTC, rempas wrote: On Monday, 20 December 2021 at 11:58:58 UTC, Tejas wrote: Ehh, it still fails; should've explicitly put the length of the array and the `extern (C)` in `main` ```d module demo; [ ... ] extern(C) /+added this because you used -bette

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 20 December 2021 at 18:03:09 UTC, rempas wrote: > Now the problem is that I want it to get the name of so > symbol and add it to a string literal. Let's check this example: enum state(alias name) = `name` ~ ` = 10;`; https://dlang.org/spec/traits.html#identifier

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 11:58:58 UTC, Tejas wrote: Ehh, it still fails; should've explicitly put the length of the array and the `extern (C)` in `main` ```d module demo; [ ... ] extern(C) /+added this because you used -betterC+/ void main() { while (true) { mixin

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 11:30:09 UTC, rumbu wrote: Enums (that's why the string is declarated as enum) are evaluated at compile time, the concatenation op will not end in your code as instruction, so you can do anything outside betterC rules as long you do it at compile time. You are j

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread Tejas via Digitalmars-d-learn
On Monday, 20 December 2021 at 11:30:09 UTC, rumbu wrote: On Monday, 20 December 2021 at 10:49:20 UTC, rempas wrote: On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote: Thanks a lot for the info. When I try to use this code, I'm getting the following error: ``` Error: expression expect

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rumbu via Digitalmars-d-learn
On Monday, 20 December 2021 at 10:49:20 UTC, rempas wrote: On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote: Thanks a lot for the info. When I try to use this code, I'm getting the following error: ``` Error: expression expected, not `%` Error: expression expected, not `%` ``` My fa

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
On Monday, 20 December 2021 at 09:30:30 UTC, rumbu wrote: because you cannot have statements directly in a template (the fact that is a mixin template is irelevant), only declarations. If you want to just insert some random code, use strings. You can create a templated enum to store your par

Re: How to insert code in place with templates/mixins?

2021-12-20 Thread rumbu via Digitalmars-d-learn
On Monday, 20 December 2021 at 08:45:50 UTC, rempas wrote: Here I am having a problem with templates again. No matter how much I read, I can't seem to understand how templates/mixins work. So any ideas why this doesn't work? because you cannot have statements directly in a tem

How to insert code in place with templates/mixins?

2021-12-20 Thread rempas via Digitalmars-d-learn
Here I am having a problem with templates again. No matter how much I read, I can't seem to understand how templates/mixins work. So I'm having the following code (just a snippet of the real code): ``` if (c != '%') { if (stdout_index < STDOUT_BUF_LEN)

Re: mixins

2021-11-21 Thread Alexey via Digitalmars-d-learn
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote: Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } }

Re: mixins

2021-11-17 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote: Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } }

Re: mixins

2021-11-17 Thread Tejas via Digitalmars-d-learn
On Wednesday, 17 November 2021 at 15:12:50 UTC, Tejas wrote: On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote: Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) {

Re: mixins

2021-11-17 Thread Tejas via Digitalmars-d-learn
On Wednesday, 17 November 2021 at 14:51:58 UTC, Abby wrote: Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } }

mixins

2021-11-17 Thread Abby via Digitalmars-d-learn
Hello I would like to create validation mixin or mixin template which would return on error. Something like this: ``` mixin template Validate(a, b) { if(a > b) { writeln("invalid input"); return false; } } bool test(a,b) { mixin Validate!(a,b); on v

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 19:31:54 UTC, DLearner wrote: if (typeof(v).stringof == "int" ) { Tip: you can instead of string of do if (is(typeof(v) == int)) That is operator lets you compare types directly. (stringof is something you will almost never use as you learn more of the lan

Re: Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:28:22 UTC, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 18:07:48 UTC, Ali Çehreli wrote: In some cases it's more useful to have a 'static if' inside a single function template instead of two separate function templates. In most cases that's better. A template constraint is really a way to say "this template cannot ac

Re: Scope of Mixins

2021-08-26 Thread Ali Çehreli via Digitalmars-d-learn
On 8/26/21 10:45 AM, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote: String mixins are appealing because they can inject code like C macros do. It's not trivially possible to do the same with template mixins. Template mixins are great, but obviously to

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 17:39:16 UTC, Ali Çehreli wrote: String mixins are appealing because they can inject code like C macros do. It's not trivially possible to do the same with template mixins. Template mixins are great, but obviously totally inappropriate here. I'm ju

Re: Scope of Mixins

2021-08-26 Thread Ali Çehreli via Digitalmars-d-learn
alias. You can just check `typeof(v)` in there. String mixins are appealing because they can inject code like C macros do. It's not trivially possible to do the same with template mixins. import std.traits : isPointer; import std.stdio : writeln; mixin template valueFrom(alias var) if (isPoin

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 17:01:06 UTC, DLearner wrote: The object was to take a variable, and do alternative things with it depending on (say) whether it was an 'int' or an 'int*'. That's *very* easy to do with the alias. You can just check `typeof(v)` in there.

Re: Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:28:22 UTC, Adam D Ruppe wrote: On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself

Re: Scope of Mixins

2021-08-26 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: You really shouldn't use string mixins like this at all. If you want to work with a variable, pass the variable itself as an argument to the function and use it with regular code inste

Re: Scope of Mixins

2021-08-26 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 26 August 2021 at 16:16:55 UTC, DLearner wrote: Please confirm that mixins of format: ``` string mxn1(string VarName) { ... } ``` Invoked like: ``` mixin(mxn1("Var1")); ``` Have a wider scope than mixins like: ``` string mxn2(string VarName)() { ... } ``` In

Scope of Mixins

2021-08-26 Thread DLearner via Digitalmars-d-learn
Please confirm that mixins of format: ``` string mxn1(string VarName) { ... } ``` Invoked like: ``` mixin(mxn1("Var1")); ``` Have a wider scope than mixins like: ``` string mxn2(string VarName)() { ... } ``` Invoked like: ``` mixin(mxn2!"Var2"); ``` I tried direct r

Re: CTFE, string mixins & code generation

2020-01-24 Thread Jesse Phillips via Digitalmars-d-learn
On Friday, 24 January 2020 at 16:21:48 UTC, Jan Hönig wrote: I am looking for a detailed explanation or showcase regarding CTFE and string mixins. I want to play with D a little bit regarding code generation. I would like to have a pseudo-AST, consisting of a few classes, to represent some

Re: CTFE, string mixins & code generation

2020-01-24 Thread Jan Hönig via Digitalmars-d-learn
;enum', but there are limitations (enums cannot take AA's or class objects as values, also, once assigned they are immutable). As long as you can produce a string (via CTFE or otherwise) whose value is known at compile-time and represents valid D code, it's fair game for string

Re: CTFE, string mixins & code generation

2020-01-24 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 24, 2020 at 08:26:50PM +0100, Jacob Carlborg via Digitalmars-d-learn wrote: > On 2020-01-24 19:43, H. S. Teoh wrote: > > > (enums cannot take AA's or class objects as values, also, once > > assigned they are immutable). > > AA enums work. Ah you're right, it's statically-constructed

Re: CTFE, string mixins & code generation

2020-01-24 Thread Jacob Carlborg via Digitalmars-d-learn
On 2020-01-24 19:43, H. S. Teoh wrote: (enums cannot take AA's or class objects as values, also, once assigned they are immutable). AA enums work. Class objects kind of work. One can use static const/immutable instead. The following snippet compiles: class A { int a = 3; } const bar =

Re: CTFE, string mixins & code generation

2020-01-24 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 24, 2020 at 04:21:48PM +, Jan Hönig via Digitalmars-d-learn wrote: > I am looking for a detailed explanation or showcase regarding CTFE and > string mixins. > I want to play with D a little bit regarding code generation. > I would like to have a pseudo-AST, consist

Re: CTFE, string mixins & code generation

2020-01-24 Thread Jan Hönig via Digitalmars-d-learn
exity when using the `new` keyword. https://dlang.org/spec/function.html#interpretation Basically the only limitation of string mixins is that a single string should evaluate to valid D code, opposed to C macros. So int y mixin("= 6"); doesn't compile, while int y = mixin("

Re: CTFE, string mixins & code generation

2020-01-24 Thread Marco de Wild via Digitalmars-d-learn
On Friday, 24 January 2020 at 16:21:48 UTC, Jan Hönig wrote: I am looking for a detailed explanation or showcase regarding CTFE and string mixins. I want to play with D a little bit regarding code generation. I would like to have a pseudo-AST, consisting of a few classes, to represent some

CTFE, string mixins & code generation

2020-01-24 Thread Jan Hönig via Digitalmars-d-learn
I am looking for a detailed explanation or showcase regarding CTFE and string mixins. I want to play with D a little bit regarding code generation. I would like to have a pseudo-AST, consisting of a few classes, to represent some calculation. Think of a loop, some statements, and expressions

"Mixin is not defined" error when trying to use template mixins

2019-10-27 Thread solidstate1991 via Digitalmars-d-learn
There's a template mixin in my dimage project's new version in base.d, and when I try to access it from another file I get two errors: undefined identifier `ChunkyAccess4bit`, did you mean template `ChunkyAccess4Bit()`? and mixin `dimage.tga.TGA.ChunkyAccess4bit!()` is not defined Should I

Re: How to pass variables to string mixins?

2019-02-25 Thread Benjamin Schaaf via Digitalmars-d-learn
On Tuesday, 26 February 2019 at 00:07:54 UTC, Victor Porton wrote: I want to create a string mixin based on a supplementary variable (name2 below): Let we have something like: mixin template X(string name) { immutable string name2 = '_' ~ name; mixin("struct " ~ name2 ~ "{ int i; }"); } B

Re: How to pass variables to string mixins?

2019-02-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 25, 2019 5:07:54 PM MST Victor Porton via Digitalmars-d- learn wrote: > I want to create a string mixin based on a supplementary variable > (name2 below): > > Let we have something like: > > mixin template X(string name) { >immutable string name2 = '_' ~ name; >mixin("st

Re: How to pass variables to string mixins?

2019-02-25 Thread Rubn via Digitalmars-d-learn
On Tuesday, 26 February 2019 at 00:07:54 UTC, Victor Porton wrote: I want to create a string mixin based on a supplementary variable (name2 below): Let we have something like: mixin template X(string name) { immutable string name2 = '_' ~ name; mixin("struct " ~ name2 ~ "{ int i; }"); } B

How to pass variables to string mixins?

2019-02-25 Thread Victor Porton via Digitalmars-d-learn
I want to create a string mixin based on a supplementary variable (name2 below): Let we have something like: mixin template X(string name) { immutable string name2 = '_' ~ name; mixin("struct " ~ name2 ~ "{ int i; }"); } But it would create variable name2 inside X, which should not be cre

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Michelle Long via Digitalmars-d-learn
On Wednesday, 12 December 2018 at 16:33:02 UTC, H. S. Teoh wrote: On Wed, Dec 12, 2018 at 01:35:00PM +, AlCaponeJr via Digitalmars-d-learn wrote: On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote: > Whoa. That looks like a compiler bug. File a bug here: > ... Genuinely asking

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/12/18 10:22 AM, Kagamin wrote: On Tuesday, 11 December 2018 at 21:19:59 UTC, Steven Schveighoffer wrote: If I add an int using a regular declaration or a straight mixin("int i0 = 5;"); then the variable shows up. mixin template genInts() {     enum arr = [0];     static foreach (t; arr

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Dec 12, 2018 at 01:35:00PM +, AlCaponeJr via Digitalmars-d-learn wrote: > On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote: > > Whoa. That looks like a compiler bug. File a bug here: > > ... > > Genuinely asking if this is a case of lacking of unit test for the > Compil

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 21:19:59 UTC, Steven Schveighoffer wrote: If I add an int using a regular declaration or a straight mixin("int i0 = 5;"); then the variable shows up. mixin template genInts() { enum arr = [0]; static foreach (t; arr) { mixin("int i0 = 5;");

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread AlCaponeJr via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 21:17:46 UTC, H. S. Teoh wrote: Whoa. That looks like a compiler bug. File a bug here: ... Genuinely asking if this is a case of lacking of unit test for the Compiler or this is a case that where is hard to test or prevent? Al.

Re: Why do ints defined in template mixins have garbage values?

2018-12-12 Thread Johannes Loher via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 21:09:55 UTC, Johannes Riecken wrote: Code: import std.conv; import std.stdio; mixin template genInts() { enum arr = [0,1]; static foreach (t; arr) { mixin("int i" ~ to!string(t) ~ " = 5;"); } } void main() { mixin genInts!(); writeln(i0); writel

Re: Why do ints defined in template mixins have garbage values?

2018-12-11 Thread H. S. Teoh via Digitalmars-d-learn
P.S. Just looked at the disassembly. Seems to be a codegen bug -- the code appears to be trying to lookup local variables (presumably i0 and i1), but somehow the initialization code is missing. --T On Tue, Dec 11, 2018 at 09:09:55PM +, Johannes Riecken via Digitalmars-d-learn wrote: > Cod

Re: Why do ints defined in template mixins have garbage values?

2018-12-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/11/18 4:09 PM, Johannes Riecken wrote: Code: import std.conv; import std.stdio; mixin template genInts() {   enum arr = [0,1];   static foreach (t; arr) {     mixin("int i" ~ to!string(t) ~ " = 5;");   } } void main() {   mixin genInts!();   writeln(i0);   writeln(i1); } Expecte

Re: Why do ints defined in template mixins have garbage values?

2018-12-11 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Dec 11, 2018 at 09:09:55PM +, Johannes Riecken via Digitalmars-d-learn wrote: > Code: > > import std.conv; > import std.stdio; > > mixin template genInts() > { > enum arr = [0,1]; > static foreach (t; arr) { > mixin("int i" ~ to!string(t) ~ " = 5;"); > } > } > > void main(

Why do ints defined in template mixins have garbage values?

2018-12-11 Thread Johannes Riecken via Digitalmars-d-learn
Code: import std.conv; import std.stdio; mixin template genInts() { enum arr = [0,1]; static foreach (t; arr) { mixin("int i" ~ to!string(t) ~ " = 5;"); } } void main() { mixin genInts!(); writeln(i0); writeln(i1); } Expected output: 5 5 Actual output is two garbage integer v

Re: Making external types available to mixins

2018-11-24 Thread John Chapman via Digitalmars-d-learn
On Friday, 23 November 2018 at 21:49:55 UTC, Kagamin wrote: Well, just have all factories in one module and import it, then they will be visible. They're part of another library over which I have no control, but yes, I could still import them all and make life easier. import allfactories; a

Re: Making external types available to mixins

2018-11-23 Thread Kagamin via Digitalmars-d-learn
On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman wrote: The following code doesn't compile because the generated type name needs to be available inside the mixin's scope, whereas it's actually in another module. auto makeWith(string className, Args…)(auto ref Args args) { mixin("r

Re: Making external types available to mixins

2018-11-23 Thread John Chapman via Digitalmars-d-learn
On Thursday, 22 November 2018 at 16:27:08 UTC, Eduard Staniloiu wrote: So I had a go at this and I have a working solution. https://run.dlang.io/is/oaH6Ib At first, I tried to do everything in the mixin, as you can see with the `failedAttempt` function. The idea was that this should have worke

Re: Making external types available to mixins

2018-11-22 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman wrote: The following code doesn't compile because the generated type name needs to be available inside the mixin's scope, whereas it's actually in another module. auto makeWith(string className, Args…)(auto ref Args args) { mixin("r

Re: Making external types available to mixins

2018-11-22 Thread Eduard Staniloiu via Digitalmars-d-learn
On Sunday, 18 November 2018 at 11:29:51 UTC, John Chapman wrote: On Saturday, 17 November 2018 at 21:11:38 UTC, Adam D. Ruppe wrote: On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman wrote: Has anyone had a similar need and come up with a solution? You might be able to just pass it

Re: Making external types available to mixins

2018-11-18 Thread John Chapman via Digitalmars-d-learn
On Saturday, 17 November 2018 at 21:11:38 UTC, Adam D. Ruppe wrote: On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman wrote: Has anyone had a similar need and come up with a solution? You might be able to just pass it the Calendar type, and then fetch its parent module and get the I

Re: Making external types available to mixins

2018-11-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 17 November 2018 at 17:58:54 UTC, John Chapman wrote: Has anyone had a similar need and come up with a solution? You might be able to just pass it the Calendar type, and then fetch its parent module and get the ICalendarFactory from there (assuming they are defined in the same mo

Re: Making external types available to mixins

2018-11-17 Thread Neia Neutuladh via Digitalmars-d-learn
On Sat, 17 Nov 2018 17:58:54 +, John Chapman wrote: > The idea is that users could type (for example) makeWith!`Calendar`(…) > instead of the longer makeWith!ICalendarFactory(…). Your project might define a hundred types named ICalendarFactory; the compiler can't figure out which one you're t

Making external types available to mixins

2018-11-17 Thread John Chapman via Digitalmars-d-learn
The following code doesn't compile because the generated type name needs to be available inside the mixin's scope, whereas it's actually in another module. auto makeWith(string className, Args…)(auto ref Args args) { mixin("return makeWith!(I", className, "Factory)(args);"); // Fowarded to i

Re: [Template] Mixins and foreach

2017-10-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 01, 2017 06:32:53 Nicholas Wilson via Digitalmars-d-learn wrote: > On Sunday, 1 October 2017 at 06:27:21 UTC, Nicholas Wilson wrote: > > And am getting > > util.d(72,12): Error: template instance helper!(foo) cannot use > > local 'foo' as parameter to non-global template > > hel

Re: [Template] Mixins and foreach

2017-09-30 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 1 October 2017 at 06:27:21 UTC, Nicholas Wilson wrote: And am getting util.d(72,12): Error: template instance helper!(foo) cannot use local 'foo' as parameter to non-global template helper(Fields...) Fixed by making helper a global template. Thanks Jonathan!

Re: [Template] Mixins and foreach

2017-09-30 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 1 October 2017 at 04:44:16 UTC, Jonathan M Davis wrote: I don't see any reason why the compiler would be complaining about 'this' being required. Neither do I. I would think that that would imply that the compiler thinks that you're accessing the member rather than introspecting on

Re: [Template] Mixins and foreach

2017-09-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 01, 2017 03:11:15 Nicholas Wilson via Digitalmars-d-learn wrote: > On Sunday, 1 October 2017 at 02:29:57 UTC, Jonathan M Davis wrote: > > I would have thought that it would be pretty straightforward to > > just write a recursive, eponymous template to solve the problem > > and h

Re: [Template] Mixins and foreach

2017-09-30 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 1 October 2017 at 02:29:57 UTC, Jonathan M Davis wrote: I would have thought that it would be pretty straightforward to just write a recursive, eponymous template to solve the problem and have it recursively build a single string to mix in for everything. In general though, without

Re: [Template] Mixins and foreach

2017-09-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 01, 2017 02:07:26 Nicholas Wilson via Digitalmars-d-learn wrote: > On Sunday, 1 October 2017 at 01:05:56 UTC, Nicholas Wilson wrote: > > struct MyType > > { > > > > void* raw; > > static struct Info > > { > > > > @(42) int foo; > > > > } > > mixin gen

Re: [Template] Mixins and foreach

2017-09-30 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 1 October 2017 at 01:05:56 UTC, Nicholas Wilson wrote: struct MyType { void* raw; static struct Info { @(42) int foo; } mixin generateGetInfo!MyTypeGetInfo; } extern(C) void MyTypeGetInfo(void*,int,size_t,void*size_t*); mixin template generateGetInfo(alias

[Template] Mixins and foreach

2017-09-30 Thread Nicholas Wilson via Digitalmars-d-learn
struct MyType { void* raw; static struct Info { @(42) int foo; } mixin generateGetInfo!MyTypeGetInfo; } extern(C) void MyTypeGetInfo(void*,int,size_t,void*size_t*); mixin template generateGetInfo(alias func) { foreach(field; typeof(this).Info.tupleof) {

Re: Inter-module symbol resolution error of template type-parameter when using mixins

2017-09-28 Thread Nordlöw via Digitalmars-d-learn
On Wednesday, 27 September 2017 at 18:24:04 UTC, Nordlöw wrote: At ... Thanks Adam for your advice at https://stackoverflow.com/questions/46454887/inter-module-symbol-resolution-error-of-template-type-parameter-when-using-mixin?noredirect=1#comment79867792_46454887 I made things work in this

Inter-module symbol resolution error of template type-parameter when using mixins

2017-09-27 Thread Nordlöw via Digitalmars-d-learn
uses `.mangleof` together with mixins to automatically infer the definition (including its name) of each array store for each element type in `Types`. The element types are passed in the template parameter `Types` to the two templated structs mentioned above. Everything works except for when I t

Re: Mixed up over mixins.

2017-08-21 Thread Johnson via Digitalmars-d-learn
you simply have not learned how to think about mixins correctly. Stop whining about it and focus that energy on working with them. [...] Thank you. You have rejuvenated my quest for mixin mastery :) Just stick with it ;) Things will click. The more you trudge through them and just try to i

Re: Mixed up over mixins.

2017-08-21 Thread Ali Çehreli via Digitalmars-d-learn
On 08/21/2017 12:29 AM, WhatMeForget wrote: > Thanks. Don't know if you noticed, but i used some code from your book. > Hope you take that as a complement. I did notice that and thank you. Every time I see people struggle with code that originated from my half-witted examples, I actually feel

Re: Mixed up over mixins.

2017-08-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/21/17 3:29 AM, WhatMeForget wrote: On Sunday, 20 August 2017 at 19:41:14 UTC, Ali Çehreli wrote: On 08/20/2017 12:27 PM, WhatMeWorry wrote: > // Mixins are for mixing in generated code into the source code. > // The mixed in code may be generated as a template instance >

Re: Mixed up over mixins.

2017-08-21 Thread WhatMeForget via Digitalmars-d-learn
On Sunday, 20 August 2017 at 22:50:40 UTC, Johnson Jones wrote: On Sunday, 20 August 2017 at 19:27:43 UTC, WhatMeWorry wrote: [...] It's not difficult, it's just new. It's not that you are a poor programmer, but you simply have not learned how to think about mixins correctl

Re: Mixed up over mixins.

2017-08-21 Thread WhatMeForget via Digitalmars-d-learn
On Sunday, 20 August 2017 at 19:41:14 UTC, Ali Çehreli wrote: On 08/20/2017 12:27 PM, WhatMeWorry wrote: > // Mixins are for mixing in generated code into the source code. > // The mixed in code may be generated as a template instance > // or a string. Yes, it means that t

Re: Mixed up over mixins.

2017-08-20 Thread Johnson Jones via Digitalmars-d-learn
`; } void main() { // Mixins are for mixing in generated code into the source code. // The mixed in code may be generated as a template instance // or a string. mixin(printStatement("hello world")); mixin(`writeln(` ~ `Hello` ~ `);` ); mixin("writeln(`World`);"

Re: Mixed up over mixins.

2017-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 08/20/2017 12:27 PM, WhatMeWorry wrote: > // Mixins are for mixing in generated code into the source code. > // The mixed in code may be generated as a template instance > // or a string. Yes, it means that the string must be legal D code. > mixin(`writel

Mixed up over mixins.

2017-08-20 Thread WhatMeWorry via Digitalmars-d-learn
It's stuff like this which makes me very frustrated. Or depressed because it demonstrates just how poor a programmer I am: string printStatement(string message) { return `writeln("` ~ message ~ `");`; } void main() { // Mixins are for mixing in generated code into t

Re: Template mixins and selective imports

2017-08-03 Thread jmh530 via Digitalmars-d-learn
On Thursday, 3 August 2017 at 19:05:47 UTC, Meta wrote: On Thursday, 3 August 2017 at 19:03:55 UTC, Meta wrote: `mixin vectorize!sin vsin; alias sin = vsin;` and see if it Should be `alias sin = vsin.sin;` Thanks, this pointed me in the right direction. I got the line below working. m

Re: Template mixins and selective imports

2017-08-03 Thread Meta via Digitalmars-d-learn
On Thursday, 3 August 2017 at 19:03:55 UTC, Meta wrote: `mixin vectorize!sin vsin; alias sin = vsin;` and see if it Should be `alias sin = vsin.sin;`

Re: Template mixins and selective imports

2017-08-03 Thread Meta via Digitalmars-d-learn
On Thursday, 3 August 2017 at 15:29:47 UTC, jmh530 wrote: I am trying to create a vectorize function that mixes in a new version of function with the same name that applies the function (to an ndslice). The code below compiles without error and has the behavior I would expect. However, when

Template mixins and selective imports

2017-08-03 Thread jmh530 via Digitalmars-d-learn
I am trying to create a vectorize function that mixes in a new version of function with the same name that applies the function (to an ndslice). The code below compiles without error and has the behavior I would expect. However, when I change the function import to a selective import (e.g.

Re: Simple c header => Dlang constants using mixins in compile time

2017-06-18 Thread Igor Shirkalin via Digitalmars-d-learn
On Sunday, 18 June 2017 at 16:02:38 UTC, Seb wrote: On Saturday, 17 June 2017 at 11:27:40 UTC, Igor Shirkalin wrote: On Saturday, 17 June 2017 at 11:23:52 UTC, Cym13 wrote: On Saturday, 17 June 2017 at 11:20:53 UTC, Igor Shirkalin wrote: [...] I'm sure others will have cleaner solutions as a

  1   2   3   4   >