String Mixins

Bill Baxter wbaxter at gmail.com
Tue Nov 17 03:01:25 PST 2009


On Mon, Nov 16, 2009 at 3:42 PM, Travis Boucher
<boucher.travis at gmail.com> wrote:
> I've been playing with string mixins, and they are very powerful.
>
> One thing I can't figure out is what exactly can and cannot be evaluated at
> compile time.
>
> For example:
>
> ----
> char[] myFunc1() {
>        return "int a = 1;";
> }
>
> char[] myFunc2() {
>        char[] myFunc3() {
>                return "int b = 2;";
>        }
>        return myFunc3();
> }
>
> void main() {
>        mixin(myFunc1());
>        mixin(myFunc2());
> }
> ----
>
> myFunc1() can be used as a string mixin.
> myFunc2() can't be.
>
> I'm sure there are other things that I'll run into, but I figure there is
> some simple set of rules of what can and can't be used as a string mixin.

Unfortunately there aren't any easy rules to go by.  If it doesn't
work CTFE, and a bug hasn't already been filed, then you could file a
bug, especially if you find the problem blocking your progress.
However, at this point there are plenty of things that don't work that
are known and being targeted by Don already.  So a flood of "this and
that don't work in CTFE" bug reports may not be so useful just yet.

Anyway, just be thankful that it now at least tells you what can't be
evaluated.  That's a vast improvement over the old days when the
compiler would just give a generic error message about CTFE and leave
you guessing about which line it didn't like!

--bb



More information about the Digitalmars-d mailing list