Thread overview
Re: mixin functions
Nov 01, 2012
Gor Gyolchanyan
Nov 02, 2012
Philippe Sigaud
Nov 02, 2012
Manu
Nov 02, 2012
Jacob Carlborg
Nov 03, 2012
Timon Gehr
November 01, 2012
OR, better yet:

mixin MyMixin
{
    foreach(i; 0..10)
        MyMixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
}

And this could be printed out as a pragma(msg, ...) or into a .log file or anywhere else. This way it's easy to see what did end up being mixed in.


On Thu, Nov 1, 2012 at 7:55 PM, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com
> wrote:

> I find myself doing this very very often and it pains me to write ugly code like this over and over again:
>
> mixin(()=>{
>
>     string result;
>     foreach(i; 0..10)
>         result ~= "writeln(" ~ to!string(i); ~ ");\n"'
>     return result;
>
> }());
>
> All it does is generates a string in a delegate, which is immediately
> called and passed to a mixin.
> Almost all mixins contain generated strings and most if them need a
> dedicated string generator.
> I don't want to propose new syntax, because we all know that new syntax is
> the last thing that will be developed, considering the number of bugs out
> there. The first thing I wanted to do was this:
>
> mixin
> {
>     foreach(i; 0..10)
>         mixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
> }
>
> I'm not suggesting this to be implemented, this is just what I
> automatically wanted to write.
> Anybody else had this kind of thoughts?
>
> --
> Bye,
> Gor Gyolchanyan.
>



-- 
Bye,
Gor Gyolchanyan.


November 01, 2012
On Thursday, 1 November 2012 at 15:59:04 UTC, Gor Gyolchanyan wrote:
> OR, better yet:
>
> mixin MyMixin
> {
>     foreach(i; 0..10)
>         MyMixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
> }
>
> And this could be printed out as a pragma(msg, ...) or into a .log file or
> anywhere else. This way it's easy to see what did end up being mixed in.
>
>

Given that template mixins are currently declared as (mixin template Foo () {...}), would it make even more sense to extend this out to a more literal interpretation of the subject line ("mixin functions")?  I'm thinking something like this:

mixin string myMixin () {
    string res;
    foreach ( i ; 0 .. 10 )
        res ~= "writeln(" ~ to!string( i ) ~ ");\n";
    return res;
}

And later using it as:
mixin myMixin();

The same as templates.  The caveat is that the compiler treats 'myMixin' as though it were a template containing only the function, so it is evaluated in the context of where it is mixed in.  Since it makes little sense to return anything other than a string, perhaps the absence of the 'template' keyword would imply a function with 'string' return type.

mixin myMixin () {
    // ...same...
}

The mixin *expression*, of course, would be unaffected. Maybe it would be of limited benefit to the OP, but it might be generally useful.  Otherwise, while ugly, the current anonymous delegate approach isn't the most horrid thing imaginable.

-- Chris Nicholson-Sauls

November 02, 2012
On Fri, Nov 2, 2012 at 12:58 AM, Chris Nicholson-Sauls <ibisbasenji@gmail.com> wrote:

> Otherwise,
> while ugly, the current anonymous delegate approach isn't the most horrid
> thing imaginable.

Btw, I think (without having compiled this), that the OP code could be
simplified slightly to:

mixin({

    string result;
    foreach(i; 0..10)
        result ~= "writeln(" ~ to!string(i); ~ ");\n"'
    return result;

});

Which is not half bad, but maybe I'm used to string mixins now ;)
November 02, 2012
On 1 November 2012 17:58, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com>wrote:

> OR, better yet:
>
> mixin MyMixin
> {
>     foreach(i; 0..10)
>         MyMixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
> }
>
> And this could be printed out as a pragma(msg, ...) or into a .log file or anywhere else. This way it's easy to see what did end up being mixed in.
>

I bumped into this today actually. It's certainly a nice idea.

I see a lot of noise about said AST macros...
I understand the idea, but I have no idea how it might look in practice.
Are there any working proposals?
I find myself using mixins waaaay too much. They're just too convenient,
and enable lots of things that just aren't possible any other way. But I
always feel really dirty! It can be so tedious dealing with all the string
mess.


November 02, 2012
On 2012-11-02 21:03, Manu wrote:

> I bumped into this today actually. It's certainly a nice idea.
>
> I see a lot of noise about said AST macros...
> I understand the idea, but I have no idea how it might look in practice.
> Are there any working proposals?

No, not for D. But there are several other languages that have AST macros:

Scala: http://scalamacros.org/
Nimrod: (templates and macros) http://nimrod-code.org/tut2.html#templates
Nemerle: http://nemerle.org/wiki/index.php?title=Macros

Nemerle also supports creating new syntax for the language.

-- 
/Jacob Carlborg
November 03, 2012
On 11/02/2012 10:30 PM, Jacob Carlborg wrote:
> On 2012-11-02 21:03, Manu wrote:
>
>> I bumped into this today actually. It's certainly a nice idea.
>>
>> I see a lot of noise about said AST macros...
>> I understand the idea, but I have no idea how it might look in practice.
>> Are there any working proposals?
>
> No, not for D. But there are several other languages that have AST macros:
>
> Scala: http://scalamacros.org/
> Nimrod: (templates and macros) http://nimrod-code.org/tut2.html#templates
> Nemerle: http://nemerle.org/wiki/index.php?title=Macros
>
> Nemerle also supports creating new syntax for the language.
>

- http://www.haskell.org/haskellwiki/Template_Haskell
- http://opendylan.org/books/dpg/db_329.html
- http://en.wikipedia.org/wiki/Common_Lisp#Macros

(not a complete list at all.)