Skip to content

Anonymous methods give errors when they have const parameters: #254

Closed

Description

@JBontes

Take the following example:

unit Test;
interface
implementation

function LogStringUsage: TArray<TStringUsage>;
begin
  comparer:= function(const left, right: TStringUsage): Integer
  //                  ^^ error ')' expected, `const` found.
    begin
      Result:= -CompareValue(left.Value, right.Value);
    end;
end;

end.

The fix:

procedure TmwSimplePasPar.AnonymousMethod;
begin
  case TokenID of
    ptFunction:
      begin
        NextToken;
        if TokenID = ptRoundOpen then begin
          FormalParameterList;
        end;
        Expected(ptColon);
        ReturnType;
      end;
    ptProcedure:
      begin
        NextToken;
        if TokenID = ptRoundOpen then begin
          FormalParameterList;
        end;
      end;
  end;
  Block;
end;

Activity

  1. Wosi commented on Oct 17, 2017

    @Wosi
    Contributor

    That's what I get in current master. Seems to work fine:

        <METHOD begin_line="149" begin_col="1" end_line="158" end_col="1" kind="function" name="LogStringUsage">
          <RETURNTYPE line="149" col="26">
            <TYPE line="149" col="26" name="TArray">
              <TYPEARGS line="149" col="33">
                <TYPE line="149" col="33" name="TStringUsage"/>
              </TYPEARGS>
            </TYPE>
          </RETURNTYPE>
          <STATEMENTS begin_line="150" begin_col="1" end_line="156" end_col="4">
            <ASSIGN line="151" col="3">
              <LHS line="151" col="3">
                <IDENTIFIER line="151" col="3" name="comparer"/>
              </LHS>
              <RHS line="151" col="14">
                <EXPRESSION line="151" col="14">
                  <ANONYMOUSMETHOD line="151" col="14">
                    <PARAMETERS line="151" col="22">
                      <PARAMETER line="151" col="29" kind="const">
                        <NAME line="151" col="29" value="left"/>
                        <TYPE line="151" col="42" name="TStringUsage"/>
                      </PARAMETER>
                      <PARAMETER line="151" col="35" kind="const">
                        <NAME line="151" col="35" value="right"/>
                        <TYPE line="151" col="42" name="TStringUsage"/>
                      </PARAMETER>
                    </PARAMETERS>
                    <RETURNTYPE line="151" col="57">
                      <TYPE line="151" col="57" name="Integer"/>
                    </RETURNTYPE>
                    <STATEMENTS begin_line="153" begin_col="5" end_line="155" end_col="8">
                      <ASSIGN line="154" col="7">
                        <LHS line="154" col="7">
                          <IDENTIFIER line="154" col="7" name="Result"/>
                        </LHS>
                        <RHS line="154" col="16">
                          <EXPRESSION line="154" col="16">
                            <UNARYMINUS line="154" col="16">
                              <CALL line="154" col="16">
                                <IDENTIFIER line="154" col="17" name="CompareValue"/>
                                <EXPRESSIONS line="154" col="30">
                                  <EXPRESSION line="154" col="30">
                                    <DOT line="154" col="34">
                                      <IDENTIFIER line="154" col="30" name="left"/>
                                      <IDENTIFIER line="154" col="35" name="Value"/>
                                    </DOT>
                                  </EXPRESSION>
                                  <EXPRESSION line="154" col="42">
                                    <DOT line="154" col="47">
                                      <IDENTIFIER line="154" col="42" name="right"/>
                                      <IDENTIFIER line="154" col="48" name="Value"/>
                                    </DOT>
                                  </EXPRESSION>
                                </EXPRESSIONS>
                              </CALL>
                            </UNARYMINUS>
                          </EXPRESSION>
                        </RHS>
                      </ASSIGN>
                    </STATEMENTS>
                  </ANONYMOUSMETHOD>
                </EXPRESSION>
              </RHS>
            </ASSIGN>
          </STATEMENTS>
        </METHOD>
  2. JBontes commented on Oct 17, 2017

    @JBontes
    Author

    I guess it's me, The "fix" is the same as the code in master. I guess I broke something somehow.
    Anyway it's fixed now.

  3. added a commit that references this issue on Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions