www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13051] New: Cannot use function literal inside struct


          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 puremagic.com
          Reporter: johnnymarler 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

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

Jul 05 2014