www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin can't access library symbols?

reply faissaloo <faissaloo gmail.com> writes:
How can I get a mixin to implicitly include the symbols from its 
surrounding context? Is this possible?
May 03 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 3 May 2019 at 17:48:50 UTC, faissaloo wrote:
 How can I get a mixin to implicitly include the symbols from 
 its surrounding context? Is this possible?
What's your big picture goal? Do you have sample code you have tried so far? Also, are you talking mixin("string") or mixin template? They work differently with symbols too.
May 03 2019
parent reply faissaloo <faissaloo gmail.com> writes:
On Friday, 3 May 2019 at 17:51:39 UTC, Adam D. Ruppe wrote:
 On Friday, 3 May 2019 at 17:48:50 UTC, faissaloo wrote:
 How can I get a mixin to implicitly include the symbols from 
 its surrounding context? Is this possible?
What's your big picture goal? Do you have sample code you have tried so far? Also, are you talking mixin("string") or mixin template? They work differently with symbols too.
My sample code is too big to be useful. The use of my mixin looks like: mixin(defState!("XEvent value;")); But it tells me it can't find the symbol XEvent despite me having used it elsewhere.
May 03 2019
next sibling parent reply Alex <AJ gmail.com> writes:
On Friday, 3 May 2019 at 17:55:19 UTC, faissaloo wrote:
 On Friday, 3 May 2019 at 17:51:39 UTC, Adam D. Ruppe wrote:
 On Friday, 3 May 2019 at 17:48:50 UTC, faissaloo wrote:
 How can I get a mixin to implicitly include the symbols from 
 its surrounding context? Is this possible?
What's your big picture goal? Do you have sample code you have tried so far? Also, are you talking mixin("string") or mixin template? They work differently with symbols too.
My sample code is too big to be useful. The use of my mixin looks like: mixin(defState!("XEvent value;")); But it tells me it can't find the symbol XEvent despite me having used it elsewhere.
It's because XEvent is not visible in the scope, you need to import the module it is contained in. mixin templates are used in the call scope, not the defining scope. You have some misunderstanding of D meta programming but you h ave not given enough info to diagnose the problem. You look to be using a string that represents a code string that will be later mixed in(as a string mixin) but you are also using a template mixin. But most likely you simply need to add an "import ..." somewhere to get XEvent in the scope. Sometimes this is a little tricky because you want to generalize it so you have to do some weird stuff and use mixin("import "~moduleName!T~";");
May 04 2019
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 4 May 2019 at 08:12:46 UTC, Alex wrote:
     mixin(defState!("XEvent value;"));
mixin templates are used in the call scope, not the defining scope.
That's true, but the code there is NOT a mixin template. mixin templates are always used mixin TemplateName; or mixin TemplateName!(Args); There are no parenthesis around it for this construct.
 Sometimes this is a little tricky because you want to 
 generalize it so you have to do some weird stuff and use 
 mixin("import "~moduleName!T~";");
If you find yourself writing code like that, it means your library is badly designed and you should fix it. There is never a need for that, if you can get the module if T, it means you *already have access to T* and can simply use it directly.
May 04 2019
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 3 May 2019 at 17:55:19 UTC, faissaloo wrote:
 But it tells me it can't find the symbol XEvent despite me 
 having used it elsewhere.
Where is elsewhere? Is it in the same module as the mixin? mixin strings like that are similar to pasting the generated code into the site of the mixin() thing, into that module, which means anything the string references needs to be visible here too. I'm assuming `defState!(x)` returns a string based on x. So when you do mixin(defState!("XEvent value;")); defState returns something like "XEvent value; some other stuff;" If you take away the quotes and just write that instead of mixin, you would get the same effect. XEvent needs to be in scope at *that* point. If you are writing import module.with.XEvent; mixin(defState!("XEvent value;")); And are still getting an error, it must be because defState is also doing something with it. I would suggest redesigning it then to take the type separately. Maybe template defState(T, string name) { ... } and you call it mixin(defState!(XEvent, "value")); because now XEvent itself is only seen outside the defState call, and inside the thing, it can just use T. but this all depends on the details of what you're doing.
May 04 2019