unit function is called before passing as argument of JavaScript function
Description
Maybe related to This issue.
I am trying to call JavaScript function which has signature of addHandler(handler: Function): void.
Using ts2fable, it converts to System.Action -> unit.
When passing unit -> unit function as handler, it emits JavaScript code that calls the function before passing as argument.
Repro code
Reproduce code: REPL.
open System
// addHandler was created by ts2fable so I cannot change the signature.
let addHandler : Action -> unit = fun a -> a.Invoke()
let handler : unit -> unit = fun _ -> ()
addHandler handler // emits addHandler(handler())
addHandler (Action handler) // emits addHandler(handler())
addHandler (Action (fun _ -> handler())) // works, but too noisy
Expected and actual results
actual JavaScript output is addHandler(handler()) but I want addHandler(handler). Is there any simple way to do this ?
Related information
- Fable version: 4.16.0
- Operating system: Linux
Hello @pjmtdw,
// addHandler was created by ts2fable so I cannot change the signature. let addHandler : Action -> unit = fun a -> a.Invoke()
Why do you say you can't change the signature?
ts2fable doesn't generate a 100% perfect code, there are things it doesn't generate when it could and others that are generated incorrectly. It is always possible to make manual improvements to the generated code.
A new project, is being worked to try work around these limitation but is not yet on par with ts2fable.
I think it is possible to trick the compiler to generate the correct code by using System.Func in a particular way:
open System
type [<AllowNullLiteral>] Test =
abstract addHandler: System.Func<unit -> unit, unit>
let test = unbox<Test> null
// Inlined function example
test.addHandler.Invoke(fun () ->
printfn "Do something"
)
// Named function example
let handler () =
printfn "Do something"
test.addHandler.Invoke(handler)
generates
export const test = defaultOf();
test.addHandler(() => {
toConsole(printf("Do something"));
});
export function handler() {
toConsole(printf("Do something"));
}
test.addHandler(() => {
handler();
});
It doesn't generate addHandler(handler) but at least it will not invoke the handler directly. However, if you need to be able to call removeHandler like supported by DOM listener function for example, you will not be able to do it because you don't have access to the reference of the function.
I hope it can help.
I am not familiar yet with the compiler internal to know if this is possible to improve Fable automatic uncurry/curry mechanism here. And we need to be careful when touching that part of Fable because it can easily breaks existing projects.
Since the JavaScript function I want to call is in in external library, I wanted to avoid editing manually because when version changes maybe we have to call ts2fable again to generate fs file.
Anyway, using System.Func<unit -> unit, unit> works as expected.
There seems drawbacks that System.Func cannot use named arguments, nor use optional argument, so it might be tedious to use for function that has complex signature but maybe the problem is solved.
Yes, it may break the existing project if this behavior will change, but someday I hope there would be clean solution to this problem.
Thanks for the help.
Since the JavaScript function I want to call is in in external library, I wanted to avoid editing manually because when version changes maybe we have to call
ts2fableagain to generate fs file. Anyway, usingSystem.Func<unit -> unit, unit>works as expected.
In cases like that, what I do in general is I write a big comment to make sure I catch up during a Git review.
Could you please tell me which library you are trying to bind and which portion of the code of that library contains this situation? This is so I can perhaps create an issue to keep track of this need in https://github.com/glutinum-org/cli (ts2fable successor) and if possible improve the situation.
@MangelMaxime
Here's the demo repo I created. I want to make a browser game using Fable and LittleJS.
The actual function I want to call is engineInit, which is the initial function to call in LittleJS engine.
This is the signature created using ts2fable and this is the example code that calls it.
Thank you.
I find it strange that they are using Function without providing the shape of them. I would have expected them do things like unit -> unit or float -> unit etc.