digitalmars.D.learn - Safe copy-paste using mixin
- drug (5/5) Aug 31 2015 I have code that is being duplicated in several places and I'd like to
- drug (2/7) Aug 31 2015 Of course I can just trasnfer these lines below, but the question remain...
- Andrea Fontana (23/37) Aug 31 2015 Just create a function that return a string with those three
- drug (4/24) Aug 31 2015 As usual in D the answer is simple.)
- cym13 (6/40) Aug 31 2015 That's what template mixins are for (although in your example
- drug (4/42) Aug 31 2015 According to these sources that I've seen before I can mixin
- Andrea Fontana (2/36) Aug 31 2015 Which is the problem in your case?
I have code that is being duplicated in several places and I'd like to use mixins to simplify code maintenance but I failed to do it. For example https://github.com/drug007/hdf5-d-examples/blob/tmp/examples/aux.d Lines 80-82, 91-93 and 99-101 are identical, how can I use mixin here? I failed to do it because mixins demand declarations, not just code.
Aug 31 2015
On 31.08.2015 13:35, drug wrote:I have code that is being duplicated in several places and I'd like to use mixins to simplify code maintenance but I failed to do it. For example https://github.com/drug007/hdf5-d-examples/blob/tmp/examples/aux.d Lines 80-82, 91-93 and 99-101 are identical, how can I use mixin here? I failed to do it because mixins demand declarations, not just code.Of course I can just trasnfer these lines below, but the question remains.
Aug 31 2015
On Monday, 31 August 2015 at 10:38:41 UTC, drug wrote:On 31.08.2015 13:35, drug 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); } }I have code that is being duplicated in several places and I'd like to use mixins to simplify code maintenance but I failed to do it. For example https://github.com/drug007/hdf5-d-examples/blob/tmp/examples/aux.d Lines 80-82, 91-93 and 99-101 are identical, how can I use mixin here? I failed to do it because mixins demand declarations, not just code.Of course I can just trasnfer these lines below, but the question remains.
Aug 31 2015
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.
Aug 31 2015
On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:On 31.08.2015 13:57, Andrea Fontana wrote:That's what template mixins are for (although in your example string mixins are better suited imho). You may want to have a look at https://blog.dicebot.lv/posts/2015/08/OOP_composition_with_mixins or of course http://dlang.org/template-mixin.htmlJust 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.
Aug 31 2015
On 31.08.2015 14:36, cym13 wrote:On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:According to these sources that I've seen before I can mixin declarations, not just arbitrary code. Now I guess it's impossible for some reason.On 31.08.2015 13:57, Andrea Fontana wrote:That's what template mixins are for (although in your example string mixins are better suited imho). You may want to have a look at https://blog.dicebot.lv/posts/2015/08/OOP_composition_with_mixins or of course http://dlang.org/template-mixin.htmlJust 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.
Aug 31 2015
On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:On 31.08.2015 13:57, Andrea Fontana wrote:Which is the problem in your case?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.
Aug 31 2015
On 31.08.2015 15:28, Andrea Fontana wrote:On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:No, in my case there is no problem, I'm curious. I guess that string mixins sometimes may look like a hack.On 31.08.2015 13:57, Andrea Fontana wrote:Which is the problem in your case?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.
Aug 31 2015
On Monday, 31 August 2015 at 12:43:25 UTC, drug wrote:On 31.08.2015 15:28, Andrea Fontana wrote: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.On Monday, 31 August 2015 at 11:06:40 UTC, drug wrote:No, in my case there is no problem, I'm curious. I guess that string mixins sometimes may look like a hack.On 31.08.2015 13:57, Andrea Fontana wrote:Which is the problem in your case?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.
Aug 31 2015
On 31.08.2015 16:30, cym13 wrote:I guessed that mixing code instead of strings let compiler checks it more strictly. But now I think I was wrong and I'm agree with you.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.
Sep 01 2015