2

Is it possible to pass data generated locally in F# to the expression that's being compiled in Funscript?

Example of what I mean:

let MyJSFunc str () =
    Globals.alert str

let myStr = File.ReadAllText "somefile.txt"

Compiler.Compiler.Compile(<@ MyJSFunc myStr () @>,noReturn=true)

If I use a static string instead of reading it from a local file it works, so my guess is that Funscript tries to compile the file reading to JS, even though the file has already been read by the time it reaches the compilation?

2
  • I think this should work - what error do you get? Commented Feb 24, 2015 at 12:25
  • Complete stack trace: pastebin.com/2dEbQdE9 Commented Feb 24, 2015 at 12:32

1 Answer 1

3

FunScript attempts to translate the code of all top-level declarations. As you mentioned, this includes thy MyJSFunc function but also the myStr value (which is not possible as it accesses the file system).

One way to solve this is to turn myStr into a local declaration. That way, the F# will store its value in the quotation (rather than storing the definition):

let MyJSFunc str () =
    Globals.alert str

let translate () =
  let myStr = File.ReadAllText "somefile.txt"
  Compiler.Compiler.Compile(<@ MyJSFunc myStr () @>,noReturn=true)

Another way to do the same thing is to use quotation splicing and create a quoted value explicitly using Expr.Value:

open Microsoft.FSharp.Quotations

Compiler.Compiler.Compile(<@ MyJSFunc (%Expr.Value(myStr)) () @>,noReturn=true)
Sign up to request clarification or add additional context in comments.

3 Comments

I see, that explains it. Do you know the reasoning behind translating all top levels declarations instead of just the declarations contained inside the expression (if that makes sense)? Maybe because types have to declared at the top level?
This is more of an F# language feature than FunScript feature - all top level bindings are represented as "calls" in quotations while all local values become "embedded values". FunScript simply tries to translate all calls. That said, it would make sense to have some attribute for calls that should be "evaluated" rather than translated.
I hadn't see this use case but Tomas' solution is very good +1. Adding the attribute to evaluate calls at compile time is also a nice proposal and shouldn't be difficult but I'm afraid users may misuse it thinking some calls can be evaluated at compile time when it's not the case. There would likely be problems too if you use a stand-alone compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.