digitalmars.D - Proposal: Dedicated-string-mixin templates/functions
- Nick Sabalausky (44/44) Feb 05 2010 Point #1: It's often been noted that string mixin syntax is ugly. Which ...
- Simen kjaeraas (7/54) Feb 05 2010 Actually, this is already in Bugzilla.
- Nick Sabalausky (11/72) Feb 05 2010 Wow, that's kinda crazy, the same exact suggestion, and honestly I swear...
- Simen kjaeraas (5/23) Feb 05 2010 Let's hope so.
- Nick Sabalausky (4/22) Feb 05 2010 Done:
- =?ISO-8859-1?Q?Pelle_M=E5nsson?= (4/48) Feb 05 2010 I agree, for the mixin template part at least.
- bearophile (4/6) Feb 08 2010 I am not sure I like the "mixin" keyword used like that, but I like the ...
- Trass3r (5/16) Feb 05 2010 Well, it's a little bit indistinctive, hard to tell if it's a normal
- Yigal Chripun (14/31) Feb 06 2010 IMO, this is a bad idea.
- Nick Sabalausky (7/39) Feb 06 2010 You don't see the current "mixin(foo(fooArgs));" as being iffy syntax?
- Chad J (30/31) Feb 05 2010 I for one really want something like that. I think I posted a
- Nick Sabalausky (31/61) Feb 05 2010 Great point! Of course, that could probably be done today as:
- Chad J (2/23) Feb 06 2010 Yep. My thoughts exactly.
- Joel Anderson (12/56) Feb 11 2010 I like having nicer mixing. My proposal was to allow templates to take
- Nick Sabalausky (4/15) Feb 11 2010 While that wouldn't be my preferred solution, I could certainly live wit...
bad thing in and of itself, but it also tends to discourage use of string mixins despite their high degree of usefulness. are either designed specifically to be used as a string mixin or specifically designed to be used as something other than a string mixin. Only rarely does a single template or function seem to be particularly useful both ways. Proposal: So how about taking a cue from Nemerle: Current: ----------------------- template foo1 { const char[] foo1 = "int a;"; } char[] foo2() { return "int b;"; } mixin(foo1!()); mixin(foo2()); ----------------------- Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); ----------------------- One consequence of this worth noting is that the current string-mixin could be trivially recreated under the proposed syntax: ----------------------- mixin char[] mixinString(char[] str) { return str; } mixinString("int a;"); ----------------------- Maybe someone not as half-asleep as I currently am can point out a clean/clever way to retrieve the string value of such a template/function without actually mixing it in, to cover the occasional cases where that actually would be useful.
Feb 05 2010
Nick Sabalausky <a a.a> wrote:is a bad thing in and of itself, but it also tends to discourage use of string mixins despite their high degree of usefulness. functions are either designed specifically to be used as a string mixin or specifically designed to be used as something other than a string mixin. Only rarely does a single template or function seem to be particularly useful both ways. Proposal: So how about taking a cue from Nemerle: Current: ----------------------- template foo1 { const char[] foo1 = "int a;"; } char[] foo2() { return "int b;"; } mixin(foo1!()); mixin(foo2()); ----------------------- Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); ----------------------- One consequence of this worth noting is that the current string-mixin could be trivially recreated under the proposed syntax: ----------------------- mixin char[] mixinString(char[] str) { return str; } mixinString("int a;"); ----------------------- Maybe someone not as half-asleep as I currently am can point out a clean/clever way to retrieve the string value of such a template/function without actually mixing it in, to cover the occasional cases where that actually would be useful.Actually, this is already in Bugzilla. ( http://d.puremagic.com/issues/show_bug.cgi?id=3666 ) And it will probably come as no surprise that I, as creator of that request, very much support this. -- Simen
Feb 05 2010
"Simen kjaeraas" <simen.kjaras gmail.com> wrote in message news:op.u7nl710qvxi10f biotronic-pc.home...Nick Sabalausky <a a.a> wrote:Wow, that's kinda crazy, the same exact suggestion, and honestly I swear I never saw that ticket before. It doesn't mention functions though. With CTFE becoming more and more powerful, CTFE is getting used more and more to generate the strings to be mixin in. So it should probably apply to functions, too. Do you think that should go in the same ticket or in a separate one? Well, considering two people independantly came up with essentially the same exact idea and the exact same reasoning, hopefully that puts more weight behind it.is a bad thing in and of itself, but it also tends to discourage use of string mixins despite their high degree of usefulness. functions are either designed specifically to be used as a string mixin or specifically designed to be used as something other than a string mixin. Only rarely does a single template or function seem to be particularly useful both ways. Proposal: So how about taking a cue from Nemerle: Current: ----------------------- template foo1 { const char[] foo1 = "int a;"; } char[] foo2() { return "int b;"; } mixin(foo1!()); mixin(foo2()); ----------------------- Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); ----------------------- One consequence of this worth noting is that the current string-mixin could be trivially recreated under the proposed syntax: ----------------------- mixin char[] mixinString(char[] str) { return str; } mixinString("int a;"); ----------------------- Maybe someone not as half-asleep as I currently am can point out a clean/clever way to retrieve the string value of such a template/function without actually mixing it in, to cover the occasional cases where that actually would be useful.Actually, this is already in Bugzilla. ( http://d.puremagic.com/issues/show_bug.cgi?id=3666 ) And it will probably come as no surprise that I, as creator of that request, very much support this.
Feb 05 2010
Nick Sabalausky <a a.a> wrote:I'd say put it in the same one. It is basically the same request.Actually, this is already in Bugzilla. ( http://d.puremagic.com/issues/show_bug.cgi?id=3666 ) And it will probably come as no surprise that I, as creator of that request, very much support this.Wow, that's kinda crazy, the same exact suggestion, and honestly I swear I never saw that ticket before. It doesn't mention functions though. With CTFE becoming more and more powerful, CTFE is getting used more and more to generate the strings to be mixin in. So it should probably apply to functions, too. Do you think that should go in the same ticket or in a separate one?Well, considering two people independantly came up with essentially the same exact idea and the exact same reasoning, hopefully that puts more weight behind it.Let's hope so. -- Simen
Feb 05 2010
"Simen kjaeraas" <simen.kjaras gmail.com> wrote in message news:op.u7n9c0nnvxi10f biotronic-pc.home...Nick Sabalausky <a a.a> wrote:Done: http://d.puremagic.com/issues/show_bug.cgi?id=3666I'd say put it in the same one. It is basically the same request.Actually, this is already in Bugzilla. ( http://d.puremagic.com/issues/show_bug.cgi?id=3666 ) And it will probably come as no surprise that I, as creator of that request, very much support this.Wow, that's kinda crazy, the same exact suggestion, and honestly I swear I never saw that ticket before. It doesn't mention functions though. With CTFE becoming more and more powerful, CTFE is getting used more and more to generate the strings to be mixin in. So it should probably apply to functions, too. Do you think that should go in the same ticket or in a separate one?
Feb 05 2010
On 02/05/2010 12:13 PM, Nick Sabalausky wrote:bad thing in and of itself, but it also tends to discourage use of string mixins despite their high degree of usefulness. are either designed specifically to be used as a string mixin or specifically designed to be used as something other than a string mixin. Only rarely does a single template or function seem to be particularly useful both ways. Proposal: So how about taking a cue from Nemerle: Current: ----------------------- template foo1 { const char[] foo1 = "int a;"; } char[] foo2() { return "int b;"; } mixin(foo1!()); mixin(foo2()); ----------------------- Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); ----------------------- One consequence of this worth noting is that the current string-mixin could be trivially recreated under the proposed syntax: ----------------------- mixin char[] mixinString(char[] str) { return str; } mixinString("int a;"); ----------------------- Maybe someone not as half-asleep as I currently am can point out a clean/clever way to retrieve the string value of such a template/function without actually mixing it in, to cover the occasional cases where that actually would be useful.I agree, for the mixin template part at least. Then we just need a cleaner way to "return" things from templates, and we'll be all good.
Feb 05 2010
Pelle Månsson:Then we just need a cleaner way to "return" things from templates, and we'll be all good.I am not sure I like the "mixin" keyword used like that, but I like the idea of a better return syntax for templates (and private attribute for names inside templates). Bye, bearophile
Feb 08 2010
Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); -----------------------Well, it's a little bit indistinctive, hard to tell if it's a normal function call or a mixin without e.g. using a mixin prefix for the function name (which is nothing better than it is now) But an advantage would be that these functions could be omitted in the final executable since they are only used at compile-time.
Feb 05 2010
On 05/02/2010 23:24, Trass3r wrote:IMO, this is a bad idea. The most important thing we should get from Nemerle regarding this is the much better compilation model and not just the syntax. The syntax idea itself is iffy at best especially in the D version. To contrast with the Nemerle solution: the "function" foo2 above would be put in a separate file and would be compiled *once* into a lib. Than, at a separate phase, this lib can be loaded by the compiler and used in the client code. Also, In Nemerle, foo2 is a regular function which means, unlike D, it isn't restricted compared to "regular" functions and for example can call stdlib functions like the equivalent of "writef" (no need for special pragma(msg, ..) constructs).Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); -----------------------Well, it's a little bit indistinctive, hard to tell if it's a normal function call or a mixin without e.g. using a mixin prefix for the function name (which is nothing better than it is now) But an advantage would be that these functions could be omitted in the final executable since they are only used at compile-time.
Feb 06 2010
"Yigal Chripun" <yigal100 gmail.com> wrote in message news:hkjnp8$ms9$1 digitalmars.com...On 05/02/2010 23:24, Trass3r wrote:You don't see the current "mixin(foo(fooArgs));" as being iffy syntax?IMO, this is a bad idea. The most important thing we should get from Nemerle regarding this is the much better compilation model and not just the syntax. The syntax idea itself is iffy at best especially in the D version.Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); -----------------------Well, it's a little bit indistinctive, hard to tell if it's a normal function call or a mixin without e.g. using a mixin prefix for the function name (which is nothing better than it is now) But an advantage would be that these functions could be omitted in the final executable since they are only used at compile-time.To contrast with the Nemerle solution: the "function" foo2 above would be put in a separate file and would be compiled *once* into a lib. Than, at a separate phase, this lib can be loaded by the compiler and used in the client code. Also, In Nemerle, foo2 is a regular function which means, unlike D, it isn't restricted compared to "regular" functions and for example can call stdlib functions like the equivalent of "writef" (no need for special pragma(msg, ..) constructs).Absolutely agree with you here. That would be a fairly involved change though, and I suspect D being natively compiled may raise more difficulties and cross-platform issues than it does in Nemerle as Nemerle is VM-only. Would certainly be nice to have that at some point though.
Feb 06 2010
Nick Sabalausky wrote:...I for one really want something like that. I think I posted a suggestion like this as well: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=95579 This also came up in another thread and was warmly received, in the context of using the syntax on sql queries: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=102085 I like your syntax too. Mostly I just want to see this happen somehow. I also realized such syntax allows us to do this: int foo = 42; string bar = "answer"; writefln("The $bar is $foo."); // Prints "The answer is 42." I'd love to be able to do that. This all still leaves the problem of parentheses matching. Parentheses make a terrible nesting element. mixin Foo! (` someDslCodeHere `); // Yes, don't forget the terribly asymmetrical semicolon. versus mixin Foo!() { // Valid D tokens only, and {} must be matched. someDslCodeHere } I think the latter is worth investing in as well.
Feb 05 2010
"Chad J" <chadjoan __spam.is.bad__gmail.com> wrote in message news:hkic29$ug7$1 digitalmars.com...Nick Sabalausky wrote:Great point! Of course, that could probably be done today as: mixin(writefln("The $bar is $foo.")); but....eeeewww!...I for one really want something like that. I think I posted a suggestion like this as well: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=95579 This also came up in another thread and was warmly received, in the context of using the syntax on sql queries: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=102085 I like your syntax too. Mostly I just want to see this happen somehow. I also realized such syntax allows us to do this: int foo = 42; string bar = "answer"; writefln("The $bar is $foo."); // Prints "The answer is 42."I'd love to be able to do that.Yea, frankly I'm not certain which way would be better. But personally I'd be very happy either way. However, we should probably take a look around the Nemerle community regarding this issue. AIUI, This proposal is essentially how Nemerle's mixins work (except they call them macros and have AST features), so it could be helpful to see if any of them have found issue with it.This all still leaves the problem of parentheses matching. Parentheses make a terrible nesting element. mixin Foo! (` someDslCodeHere `); // Yes, don't forget the terribly asymmetrical semicolon. versus mixin Foo!() { // Valid D tokens only, and {} must be matched. someDslCodeHere } I think the latter is worth investing in as well.D2 has q{}: mixin Foo!( q{ someDslCodeHere }); But yea, that's is still a good point. Although I think it might be better considered part of the more general problem of passing code literals (even though it's as delegates in one case and as strings in the other). For instance, even without any mixins involved, we still have this kind of mess: void repeat(int num, IForgetTheSyntaxOffhandButLetsJustSayADgThatTakesVoidAndReturnsVoid dg) { for(int i; 0..num) dg(); } // Eeew: repeat(3, { // Stuff here });
Feb 05 2010
Nick Sabalausky wrote:... But yea, that's is still a good point. Although I think it might be better considered part of the more general problem of passing code literals (even though it's as delegates in one case and as strings in the other). For instance, even without any mixins involved, we still have this kind of mess: void repeat(int num, IForgetTheSyntaxOffhandButLetsJustSayADgThatTakesVoidAndReturnsVoid dg) { for(int i; 0..num) dg(); } // Eeew: repeat(3, { // Stuff here });Yep. My thoughts exactly.
Feb 06 2010
On 2/5/2010 3:13 AM, Nick Sabalausky wrote:bad thing in and of itself, but it also tends to discourage use of string mixins despite their high degree of usefulness. are either designed specifically to be used as a string mixin or specifically designed to be used as something other than a string mixin. Only rarely does a single template or function seem to be particularly useful both ways. Proposal: So how about taking a cue from Nemerle: Current: ----------------------- template foo1 { const char[] foo1 = "int a;"; } char[] foo2() { return "int b;"; } mixin(foo1!()); mixin(foo2()); ----------------------- Proposed: ----------------------- mixin template foo1 { const char[] foo1 = "int a;"; } mixin char[] foo2() { return "int b;"; } foo1!(); foo2(); ----------------------- One consequence of this worth noting is that the current string-mixin could be trivially recreated under the proposed syntax: ----------------------- mixin char[] mixinString(char[] str) { return str; } mixinString("int a;"); ----------------------- Maybe someone not as half-asleep as I currently am can point out a clean/clever way to retrieve the string value of such a template/function without actually mixing it in, to cover the occasional cases where that actually would be useful.I like having nicer mixing. My proposal was to allow templates to take a string. That would make the mixin both visible but still make it look neat. void mixinString(string T)() { mixin(func(T)); } On the call site... mixinString!("int a;"); The extra ! indicates that the string must be compile time constant because its going into a template.
Feb 11 2010
"Joel Anderson" <ask me.com> wrote in message news:hl1cdn$2fkn$1 digitalmars.com...I like having nicer mixing. My proposal was to allow templates to take a string. That would make the mixin both visible but still make it look neat. void mixinString(string T)() { mixin(func(T)); } On the call site... mixinString!("int a;"); The extra ! indicates that the string must be compile time constant because its going into a template.While that wouldn't be my preferred solution, I could certainly live with that and be very happy.
Feb 11 2010