I am interested in the ability to have F# scripting within my app.
Having something like tryfsharp.org would be great, specifically the Intellisense capability. Is any of this available on Github somewhere? How does this work?
Short answer
The code used for the first cut of TryFSharp with F# 2, which includes Intellisense support, is available as a code drop. As an example Rob Pickering built an online editor for Undertone with it. I suspect the code used on the current TryFSharp site which uses F# 3 will appear in time.
TryFSharp uses Silverlight to host the F# compiler in the client's browser (F# is written in F#). It is also possible to call an instance of the F# compiler running on the server from the browser on demand, which is an approach taken by TryFs.Net and Pit.
Longer answer
There are two sides to scripting:
F# already supports editing and execution of (.fsx) script files via F# Interactive.
Editing F# Code
There's no shortage of external editors for F# code:
The editor support for Xamarin Studio, Emacs and Vim is based on the open source F# Bindings project, which provides code completion. SharpDevelop uses the open source AvalonEdit and includes syntax highlighting for F#. You can use AvalonEdit in your own projects, for example the open source Refunctor project uses it to provide F# editing inside Reflector.
There are also a couple of new editors for F# on the horizon:
AvalonEdit is a good place to start for a desktop based embedded editor. Once you've chosen an editor environment then you need to choose between simple syntax highlighting or more advanced integration using F# Bindings. If you suspect people will use an external editor then syntax highlighting may be sufficient.
Bring your own editor is probably the easiest place to start which just leaves execution.
Executing F# Code
Options for executing F# code:
Compiling a snippet with the F# CodeDOM:
open Microsoft.FSharp.Compiler.CodeDom
open System.CodeDom.Compiler
let compile snippet =
use provider = new FSharpCodeProvider()
let options = CompilerParameters(GenerateInMemory=true)
provider.CompileAssemblyFromSource(options, [|snippet|])
let snippet = """
module Snippet
let x = 1
"""
let results = compile snippet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With