[JSCodegen] Assign a procedure or a function to a variant variable
Hello Eric, how can we assign a procedure or a funtion (with one or more arguments) to a variant variable please?
Something like
var procVar: variant;
var proc := procedure (s: String)
begin
end;
procVar := proc; //COMPILATION ERROR
var funcVar: variant;
var func := function (s1, s2: String): string
begin
result := s1 + s2;
end;
procFunc := func; //COMPILATION ERROR
Something like this will work on the JS Codegen:
var procVar: variant;
var proc := procedure (s: String)
begin
end;
asm @procVar = @proc; end;
var funcVar: variant;
var func := function (s1, s2: String): string
begin
result := s1 + s2;
end;
asm @funcVar = @func; end;
Normally, on JS side, assign a function or procedure to a variant is not an error.
Comments (11)
-
repo owner -
repo owner - changed status to closed
-
reporter Hi Eric, I just tested your snippets but there are errors:
Syntax Error: unexpected "@" [line: 129, column: 14, file: form1] Syntax Error: More arguments expected [line: 129, column: 19, file: form1] Syntax Error: Incompatible types: "Variant" and "procedure (String)" [line: 129, column: 14, file: form1] Syntax Error: unexpected "@" [line: 136, column: 14, file: form1] Syntax Error: More arguments expected [line: 136, column: 19, file: form1] Syntax Error: Incompatible types: "Variant" and "function (String, String): String" [line: 136, column: 14, file: form1]
I’m using it with the JS Codegen.
-
repo owner HI, which version are you using ? Latest version or something older ?
-
reporter Hi Eric, sorry but I cannot find the version number… In the file dwsCompiler.pas, it’s 20190215.0 but in you repo, it’s like that too.
-
repo owner Hmm, this version number was more meant so that ifdef’ing could be used to work around language compatibility changes (new supported constructs, etc.)
Updating to latest repository version should fix your issue AFAICT
-
reporter Just downloaded from the repo and compiled the demos “Live Scripting IDE”. It’s the same thing. Cannot compile.
Version of DWS is 2.3 on it.
-
repo owner Looks like it is a bug of the demo, which may have been hidden on older (slower) CPUs: the thread that is used to trigger automatic compilation is firing before the Script component has been setup, which prevents the JS language extension from installing properly.
-
repo owner I have pushed the fix for the demo.
-
reporter Yes!!! Now it’s fine. Thank you.
One working trick also is Variant(@func).
var procVar: variant; var proc1 := procedure (s: String) begin asm console.log(@s); end; end; procVar := variant(@proc1); procVar.call(nil, "Test"); var proc2 := procedure (s1, s2: String) begin asm console.log((@s1) + " " + (@s2)); end; end; procVar := variant(@proc2); procVar.call(nil, "Test1", "Test2");
-
repo owner - changed status to resolved
- Log in to comment
Hi, you need to explicit with an “@" operator.
When the left-side is a function pointer, the @ is optional (like in Delphi), but for a variant, there is an ambiguity.
Since at some point there could be an overload of the func/proc introduced with zero parameters, the meaning of the same assignment could change to function result rather than function pointer.
To avoid that the @ is required to explicit the code in case of ambiguity.
So it would be in your snippets