String Mixins & Compile Time Evaluation

Bill Baxter wbaxter at gmail.com
Tue Nov 17 04:42:10 PST 2009


On Tue, Nov 17, 2009 at 4:17 AM, Don <nospam at nospam.com> wrote:
> Bill Baxter wrote:
>>
>> On Tue, Nov 17, 2009 at 2:07 AM, Don <nospam at nospam.com> wrote:
>>>
>>> Travis Boucher 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 think you're using an old version of DMD. It's been working since
>>> DMD1.047. Please upgrade to the latest version, you'll find it a lot less
>>> frustrating.
>>> The bottom of "function.html" in the spec gives the rules.
>>> Though it says nested functions aren't supported, but they are.
>>
>> Ah, forgot about that list.  Good point.
>> But still Travis should know that the list is not exhaustive.
>> For instance the other day I found that this didn't work for some reason:
>>
>>   while(i < a - b) { ... }
>>
>> Instead I had to do
>>
>>   int limit = a-b;
>>   while(i < limit) { ... }
>
> I can't reproduce it. Do you have a complete test case?

Doh!  Now I can't either.  Maybe it was just a typo that I didn't notice.

How about C functions?  Any chance those'll ever work?  Particularly C
stdlib functions.  They're one of the major things I'm finding
prevents std.string functions from being used CTFE.

--bb


More information about the Digitalmars-d-learn mailing list