IInfo needs a property that indicates if the object holds a value

Issue #36 resolved
Anders Melander created an issue

In methods and functions that take optional parameters I have previously tested IInfo.Value against Unassigned to determine if a parameter holds a value:

procedure TDataModuleMyStuff.dwsUnitBlahFooMethodsGetValueEval(Info: TProgramInfo; ExtObject: TObject);
begin
    if (Info.Params[0].Value = Unassigned) then
      // ...do something when parameter isn't supplied...
end;

After some recent changes this test now fails with a variant conversion error (can't compare a variant of type varEmpty with one of type varUnknown). Fair enough.

So instead I'm now doing this:

procedure TDataModuleMyStuff.dwsUnitBlahFooMethodsGetValueEval(Info: TProgramInfo; ExtObject: TObject);
begin
    if (VarIsEmpty(Info.Params[0].Value)) then
      // ...do something when parameter isn't supplied...
end;

However IMO both solutions are wrong as they rely on implementation details of IInfo's use of variants.

It would be better if IInfo provided a boolean property that indicated if it held a value or not. Something like IInfo.Assigned, IInfo.Empty or IInfo.IsNull.

Comments (2)

  1. Anders Melander reporter

    Expanding on the above, for some parameter types it is currently possible to test the value IInfo.ScriptObj against nil:

    if (Info.Params[0].ScriptObj <> nil) then
      ...
    

    However this is not possible for the function parameter type TInfoFunc.

    Given the following script declaration:

    type TCallback = procedure;
    procedure Test(Callback: TCallback = nil);
    

    the Delphi-side test for a specified parameter will fail. Since TInfoFunc.FScriptObj is nil the call to TInfoData.GetScriptObj will end up calling TInfo.GetScriptObj which raises an exception: Operation "IInfo.Obj" not possible on a symbol of type blah blah... (the error message probably needs to be brought up to date, btw).

  2. Log in to comment