Local Methods

The method statement can be used to declare and implement a new local method in the current scope. The method will be available for all code below its declaration, up until the end of the current scope – that is either to the end of the current method, if the variable is declared on that level, or to the end of the block Statement that includes the declaration.

The method's code has access to everything that is in scope at the point of its declaration, including type members, as well as any local Variables, Properties, Contants or other local methods declared before it.

The method declaration follows the same structure as regular Methods declared as Type Members, with a method header that can include optional parameters and result type, and a begin/end section encompassing the method body with optional require and/or ensure sections to evaluate pre- post-conditions.

method MyClass.XMarksTheSpot; // regular type member method
begin
  var x := 5;
  DoSomething(x);

  method UpdateX: Boolean; // nested Local Method
  begin
    inc(x, 5);
    result := x < 30;
  end;

  for i := 0 to 10 do
    if not UpdateX then
      break;

  DoSomethingElse(x);
end;

Please refer to the Methods topic in the Type Members section for a complete overview of method syntax.

Modifiers

No modifiers are allowed on local method declarations.

See Also