Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if function param is undefined in SmartMS?

How can I check if a function param is undefined?

procedure Test(aValue: TObject);
begin
  if aValue <> nil then
    ShowMessage('param filled')      <-- also when Test() is executed!
  else
    ShowMessage('no param filled')   <-- not called, only when Test(nil) is called
end;

However when this function is called in pure JS without a param, then aValue = undefined, but the <> nil check is converted to == null!

For example, when you have a JS function with a callback:

type
  TObjectProcedure = procedure(aValue: TObject);

procedure FetchUrlAsync(aUrl: string; aCallback: TObjectProcedure )
begin
  asm
    $().load(@aUrl, @aCallback);
  end;
end;

You can call this function with:

FetchUrlAsync('ajax/test.html', Test);

It is now depended on jQuery if "Test" is called with a param or not.

like image 463
André Avatar asked Mar 04 '26 15:03

André


1 Answers

In the next version, you'll be able to use Defined() special function, it will make a strict check against undefined (it will return true for a null value).

if Defined(aValue) then
   ...

In the current version you can define a function to check that

function IsDefined(aValue : TObject);
begin
   asm
      @result = (@aValue!==undefined);
   end;
end;
like image 128
Eric Grange Avatar answered Mar 08 '26 05:03

Eric Grange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!