Safe copy-paste using mixin

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 31 06:30:50 PDT 2015


On Monday, 31 August 2015 at 12:43:25 UTC, drug wrote:
> On 31.08.2015 15:28, Andrea Fontana wrote:
>> On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:
>>> On 31.08.2015 13:57, Andrea Fontana wrote:
>>>>
>>>> Just create a function that return a string with those three 
>>>> lines and
>>>> mixin it!
>>>>
>>>> Like:
>>>>
>>>> import std.stdio;
>>>>
>>>> string toMix( string a, string b, string c)
>>>> {
>>>>      return `string a = "` ~ a ~ `";` ~ `string b = "` ~ b ~ 
>>>> `";`
>>>> `string c = "` ~ c ~ `";`;
>>>> }
>>>>
>>>>
>>>> void main()
>>>> {
>>>>
>>>>      {
>>>>          mixin(toMix("hello", " world", " 1"));
>>>>          writeln(a,b,c);
>>>>      }
>>>>
>>>>      {
>>>>          mixin(toMix("hello", " world", " 2"));
>>>>          writeln(a,b,c);
>>>>      }
>>>> }
>>> As usual in D the answer is simple.)
>>>
>>> But sometimes string mixins aren't desired, it would be nice 
>>> to mixin
>>> the code, not strings.
>>
>> Which is the problem in your case?
> No, in my case there is no problem, I'm curious. I guess that 
> string mixins sometimes may look like a hack.

IMHO they are a hack. That's why they should be used with caution 
(and why using them feels so good ^_^ ). But I don't see how 
mixing arbitrary code instead of string would make them less a 
hack. Template mixin allow only declarations for exactly that 
reason AFAIK.


More information about the Digitalmars-d-learn mailing list