How properly get overloaded constructor

Issue #173 new
Lukasz De created an issue

I added an class object with methods and two overloaded constructors with different arguments:

with UnitScript.Classes.Add do
begin
  Name := 'TSheet';

  with Constructors.Add do
  begin
    Name := 'Create';
    Overloaded := True;
    OnEval := EvalAssignExternalObject(procedure(Info: TProgramInfo; var ExtObject: TObject)
      begin
        ExtObject := TScriptSheet.Create(Info.ValueAsString['AName']);
      end);
    with Parameters.Add do
    begin
      Name := 'AName';
      DataType := 'String';
    end;
  end;

  with Constructors.Add do
  begin
    Name := 'Create';
    Overloaded := True;
    OnEval := EvalAssignExternalObject(procedure(Info: TProgramInfo; var ExtObject: TObject)
      begin
        ...
        ExtObject := TScriptSheet.Create(tmpSheet);
      end);
    with Parameters.Add do
    begin
      Name := 'AContours';
      DataType := 'TContours';
    end;
    with Parameters.Add do
    begin
      Name := 'AName';
      DataType := 'String';
      DefaultValue := 'Sheet';
    end;
  end;

  with Methods.Add do
  begin
    Name := 'GetName';
    Kind := mkFunction;
    Visibility := cvPrivate;
    ResultType := 'String';
    OnEval := EvalMethod(procedure(Info: TProgramInfo; ExtObject: TObject)
      begin
        Info.ResultAsString := (ExtObject as TScriptSheet).GetName;
      end);
  end;

  with Methods.Add do
  begin
    Name := 'SetName';
    Kind := mkProcedure;
    Visibility := cvPrivate;
    OnEval := EvalMethod(procedure(Info: TProgramInfo; ExtObject: TObject)
      begin
        (ExtObject as TScriptSheet).SetName(Info.ValueAsString['AValue']);
      end);
    with Parameters.Add do
    begin
      Name := 'AValue';
      DataType := 'String';
    end;
  end;
  with Properties.Add do
  begin
    Name := 'Name';
    DataType := 'String';
    ReadAccess := 'GetName';
    WriteAccess := 'SetName';
  end;
end;

I added another object with a method that calls the constructor from the object above:

with UnitScript.Classes.Add do
begin
  Name := 'TSheets';

  with Methods.Add do
  begin
    Name := 'AddSheet';
    Kind := mkFunction;
    ResultType := 'TSheet';
    OnEval := EValMethod(procedure(Info: TProgramInfo; ExtObject: TObject)
      begin
        ...
        tmpSheet.ScriptObj := Info.Vars['TSheet'].GetConstructor('Create', tmpSheet).Call([tmpSheet.Name]).Value;
        Info.ResultAsVariant := tmpSheet.ScriptObj;
      end);
  end;
end;

I have exception in line Info.Vars['TSheet'].GetConstructor('Create', tmpSheet):

Debugger Exception Notification

Project Test.exe raised exception class Exception with message 'Invalid number of parameters (1 instead of 2) to call function constructor Create(array of TContour, String)'.

Exception is because GetConstructor does not take into account the types and number of arguments.

function TSymbolTable.FindLocal will return the first found element matching only the name. In this case return TSymbol to constructor with two arguments.

It all depends on the number of methods in the class type.

Comments (0)

  1. Log in to comment