Bug 13051 – Cannot use function literal inside struct initializer
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2014-07-06T00:01:24Z
Last change time
2018-10-10T22:19:15Z
Assigned to
No Owner
Creator
Jonathan Marler
Comments
Comment #0 by johnnymarler — 2014-07-06T00:01:24Z
It appears that function literals cannot be used within struct initializers. It seems to result in a syntax error if then function literal contains any code. I've provided the following code to demonstrate the issue.
void main()
{
struct UseConstructor
{
void function() fp;
this(void function() fp) {
this.fp = fp;
}
}
// Compiles fine
UseConstructor s1 = UseConstructor((){int a = 1;a += 24;});
struct UseInitializer
{
void function() fp;
}
// Compiles fine
void function() fp1 = (){int a = 1;a = a * 7;};
UseInitializer s2 = {fp:fp1};
// Compiles fine
UseInitializer fs3 = {fp:(){}};
// Fails
UseInitializer fs4 = {fp:(){int a = 1;}};
// It seems that the code inside the function literal is causing a syntax error
}
The error messages from compilation appear as:
Error: found '}' when expecting ';' following statement
Error: semicolon expected, not 'EOF'
Error: found 'EOF' when expecting '}' following compound statement
Comment #1 by ralph.bariz — 2016-10-16T14:26:07Z
Confirming this issue using
------------------------------------------
DMD64 D Compiler v2.071.2
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
------------------------------------------
on Linux x86_64
Comment #2 by n8sh.secondary — 2018-10-10T21:54:30Z
Tested the code in the bug report. It compiles fine.
Comment #3 by johnnymarler — 2018-10-10T22:19:15Z
Thanks for checking Nathan. I just tried this as well, works on dmd 2.082, but not 2.079, so something must have come in between those 2 version to fix this.