What about an identifier that is an mixin

Bauss via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 15 23:52:27 PST 2017


On Friday, 13 January 2017 at 21:32:49 UTC, Daniel Kozák wrote:
> Daniel Kozák <kozzi11 at gmail.com> napsal Pá, led 13, 2017 v 
> 10∶29 :
>> André Puel via Digitalmars-d <digitalmars-d at puremagic.com> 
>> napsal Pá, led 13, 2017 v 10∶15 :
>>> One thing that I miss sometimes when doing meta programming 
>>> is being able to hide that a function should be called with 
>>> mixin.
>>> 
>>> For example (pardon my lack of creativity):
>>> 
>>>     // Instead of
>>>     string declare_a() {
>>>         return "int a;"
>>>     }
>>> 
>>>     int func() {
>>>         mixin(declare_a);
>>>         return a;
>>>     }
>>> 
>>>     // Could we have?
>>>     mixin declare_a() {
>>>         return "int a;";
>>>     }
>>> 
>>>     int func() {
>>>         declare_a;
>>>         return a;
>>>     }
>>> 
>>> I think this could be useful when one is creating Idiom and 
>>> Patterns, you could hide implementations details.
>> 
>> You can do this:
>> 
>> mixin template declare_a()
>> {
>>     int a;
>> }
>> 
>> int func()
>> {
>>     mixin declare_a;
>>     return a;
>> }
>> 
>> but there is no way to exclude mixin before calling declare_a, 
>> there is a good reason for that (it is really important to be 
>> able to tell when you use mixin and when not)
>
> Right now you can even use
> template declare_a() {...}
>
> there is no difference between template and mixin template

That's not true.

Templates do not carry context with them, they only have their 
own scope available.

Where as mixin templates can access every member that is in their 
scope.

Consider:

template SetXCTFE_Template(int value) {
     void SetXCTFE_Template() {
         x = value;
     }
}

mixin template SetXCTFE_Mixin_Template(int value) {
     void handle() {
         x = value;
     }
}

void main() {
	int x;

	x = SetXCTFE_Template!10; // Not ok ...

	mixin SetXCTFE_Mixin_Template!10; // ok ...
	handle(); // ok ...
}


More information about the Digitalmars-d mailing list