Bug in using string mixins inside of a struct?
Joseph Cassman
jc7919 at outlook.com
Tue Mar 26 16:31:42 PDT 2013
On Tuesday, 26 March 2013 at 23:22:03 UTC, John Colvin wrote:
> On Tuesday, 26 March 2013 at 23:00:40 UTC, Timon Gehr wrote:
>> On 03/26/2013 10:28 PM, Joseph Cassman wrote:
>>> I get these errors
>>>
>>> aggregate.d(11): Error: variable aggregate.A.c!("y").c cannot
>>> use
>>> template to add field to aggregate 'A'
>>> aggregate.d(6): Error: template instance aggregate.A.c!("y")
>>> error
>>> instantiating
>>>
>>> from compiling the following code
>>>
>>> struct A
>>> {
>>> void b()
>>> {
>>> size_t y;
>>> mixin(c!("y"));
>>> }
>>>
>>> template c(string x)
>>> {
>>> const char[] c = "
>>> while(" ~ x ~ " < 100)
>>> {
>>> " ~ x ~ "++;
>>> }";
>>> }
>>> }
>>>
>>> I can only find bug 276
>>> (http://d.puremagic.com/issues/show_bug.cgi?id=276) which
>>> seems related
>>> but looks like it was fixed.
>>>
>>> I am using dmd 2.062 on Ubuntu Linux 12.10.
>>>
>>> Is this a bug? Or maybe bad code?
>>>
>>
>> Bad code. Use enum for compile-time constants.
>>
>> This will work:
>>
>> struct A{
>> void b(){
>> size_t y;
>> mixin(c!("y"));
>> }
>>
>> template c(string x){
>> enum c = "while(" ~ x ~ " < 100){" ~ x ~ "++;}";
>> }
>> }
>
> Perhaps you can enlighten me:
>
> why does "const c" work if the template is outside the struct,
> but not if it's inside?
>
> Also, why doesn't "string c" work even outside of the struct,
> seeing as it's value is completely defined at compile-time?
Interesting. Didn't catch the difference a minute ago.
I am interested too in how and why they get treated differently.
Joseph
More information about the Digitalmars-d
mailing list