Pushing D's mixin to the limits: Project Euler Problem 61 from Ruby to D by David Oftedal

Philippe Sigaud via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Jun 12 22:01:04 PDT 2014


On Thu, Jun 12, 2014 at 10:35 PM, Rounin via Digitalmars-d-announce
<digitalmars-d-announce at puremagic.com> wrote:
> Hey there!

Oh cool, thanks for answering!

> Yeah, to expect people to register on LiveJournal in this age of Facebook...
> Sorry about that; It must have been to deter the spammers.

Sorry for whining about it. I'm not on Facebook either, I just find it
a bit bothersome to have to register every time I want to let a
comment. I suppose I'm too used to always commenting in the same
places and am leery to getting outside my comfort zones :-)


> Thanks for taking the time to comment! Your solution with the single call to
> mixin() is much more elegant. I made a version 2 which uses it.
>
> The reason I used a double mixin() was that the first thing I tried was
> mixin("foreach() etc. etc."), which I think may have failed due to the
> foreach, then mixin("defineMain();"); , which was interpreted as a function
> declaration, and then I went straight for the mixin("mixin()"); . If it
> ain't broke, et cetera.
>
> I still think the double mixin() can compete in terms of comedy value,
> though, don't you think?

mixin("..."); pastes the inside code where the mixin is. So there is
no real reason to mix a pure string like mixin("defineMain()"), since
you could directly write it into your code. As for foreach, I guess
it's because foreach is not a declaration and you cannot put it at the
module level:

module foo;

foreach(i; 0..10) {}

is illegal code, I suppose. Mixin or not.

In fact, you'll probably never see

mixin("some predefined code");

what is used is

mixin("some code" ~ some external value ~ "more code");
or
mixin(foo(args));

where foo returns a string.


> Also, thanks for pointing out UFCS. It seems like a very convenient, not to
> mention SANE alternative to monkey patching, and it'll make it even more
> seamless to port code that makes use of that mechanism.

Cool! Again, thanks for an interesting article.


More information about the Digitalmars-d-announce mailing list