Constant function/delegate literal

Don Clugston dac at nospam.com
Tue Jan 17 08:02:34 PST 2012


On 15/01/12 20:35, Timon Gehr wrote:
> On 01/14/2012 07:13 PM, Vladimir Matveev wrote:
>> Hi,
>>
>> Is there a reason why I cannot compile the following code:
>>
>> module test;
>>
>> struct Test {
>> int delegate(int) f;
>> }
>>
>> Test s = Test((int x) { return x + 1; });
>>
>> void main(string[] args) {
>> return;
>> }
>>
>> dmd 2.057 says:
>>
>> test.d(7): Error: non-constant expression cast(int
>> delegate(int))delegate pure
>> nothrow @safe int(int x)
>> {
>> return x + 1;
>> }
>>
>> ?

The 'this' pointer in the delegate:
Test((int x) { return x + 1; });

is a pointer to the struct literal, which isn't constant. You actually 
want it to point to variable s.

OTOH if it were a function, it should work, but it currently doesn't.

>>
>> This is simple example; what I want to do is to create a global variable
>> containing a structure with some ad-hoc defined functions. The compiler
>> complains that it "cannot evaluate .... at compile time". I think this
>> could
>> be solved by defining a function returning needed structure, but I
>> think this
>> is cumbersome and inconvenient.
>>
>> Best regards,
>> Vladimir Matveev.
>
> I think it should work. I have filed a bug report:
> http://d.puremagic.com/issues/show_bug.cgi?id=7298
>
>



More information about the Digitalmars-d-learn mailing list