[Issue 13051] New: Cannot use function literal inside struct initializer
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sat Jul 5 17:01:24 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=13051
Issue ID: 13051
Summary: Cannot use function literal inside struct initializer
Product: D
Version: D2
Hardware: All
OS: Windows
Status: NEW
Severity: normal
Priority: P1
Component: DMD
Assignee: nobody at puremagic.com
Reporter: johnnymarler at gmail.com
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
--
More information about the Digitalmars-d-bugs
mailing list