Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In application script hosting. How does tryfsharp.org work?

Tags:

f#

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?

like image 916
Dave Avatar asked Mar 09 '13 16:03

Dave


1 Answers

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:

  1. Editing
  2. Execution

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:

  • Visual Studio
  • SharpDevelop
  • Xamarin Studio
  • Emacs
  • Vim
  • TryFSharp
  • F# Notebook

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:

  • Cloud Sharper - web based F# IDE
  • Tsumani IDE - embedded editor for Excel, Hadoop, etc.

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:

  • F# CodeDOM from the F# PowerPack
  • F# Compiler via the F# compiler code drop or invoking fsi.exe

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
like image 200
Phillip Trelford Avatar answered Nov 15 '22 10:11

Phillip Trelford