ghkweon / dwscript

Automatically exported from code.google.com/p/dwscript
0 stars 0 forks source link

Events in scripts #395

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In script I define event type and class with this event:

------------------------------
type
 TOnGetValue = function : string of object;

type
  TTestClass = class
   private
    fOnGetValue: TOnGetValue;
   public
    function Get : string;
    property OnGetValue: TOnGetValue read fOnGetValue write fOnGetValue;
  end;

function TTestClass.Get: string;
begin
 if Assigned(fOnGetValue) then
  Result := fOnGetValue   //LINE WITH COMPILE ERROR!
 else
  Result := 'event not assigned';
end;
------------------------------

...and next I define working class:

------------------------------
type

TMainClass = class
 function DoIt : string;
 function GetValue : string;
end;

function TMainClass.GetValue : string;
begin
 result := 'passed';
end;

function TMainClass.DoIt : string;
begin
 var Test : TTestClass = TTestClass.Create;
 try
 Test.OnGetValue := GetValue; 
 result := Test.Get;
 finally
 Test.free
 end;
end;
------------------------------

and I use main class:

------------------------------
begin
 var M : TMainClass = TMainClass.Create;
 Print(M.DoIt);
end;
------------------------------

In line where I call event method I get error:

Syntax Error: Incompatible types: Cannot assign "function TOnGetValue: String" 
to "String"

DWS not support events?

Original issue reported on code.google.com by jac....@gmail.com on 15 May 2013 at 5:58

GoogleCodeExporter commented 8 years ago
If I call this: 

Result := fOnGetValue()

this works fine. 
But do without brackets () must be a error?

Original comment by jac....@gmail.com on 16 May 2013 at 6:40

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r2093.

Original comment by zar...@gmail.com on 16 May 2013 at 7:26

GoogleCodeExporter commented 8 years ago
The implicit call on a function pointer, stored in a field, with no parameters, 
wasn't recognized.

In practice there is a lot of ambiguity with function pointers/delegates in the 
Pascal language, I wonder if at some point it couldn't be worth it hinting 
about an explicit @ to acquire a pointer and () for calls. Anyway, fixed now & 
added test.

Original comment by zar...@gmail.com on 16 May 2013 at 7:33