Bind variable to interface type in FSI

78 views Asked by At

I'm experimenting with integration of F# as scripting language and have an issue with FsiEvaluationSession.AddBoundVariable method. Problem is that this method creates variable of actual type of object, but I need to create variable of interface that it implements. I can't find AddBoundVariable<T>(string, T) or any other overload that would allow be do that.

// located in common assembly
type IFoo =
    abstract Foo : unit -> unit

type FooImpl() =
    interface IFoo with
        member _.Foo () = ()

// located in host
session.AddBoundVariable ("foo", Foo())

session.EvalInteraction "foo.Foo()" // throws, `FooImpl` type doesn't have `Foo` method

session.EvalInteraction """
let foo : IFoo = foo
foo.Foo()
""" // throws, `IFoo` not found

Question is: how can I create variable of type that I want?

1

There are 1 answers

2
Brian Berns On

You have to explicitly cast the Foo instance to IFoo, so this should work:

session.EvalInteraction """
let foo = foo :> IFoo
foo.Foo()
"""

To avoid the indirection of FSI, you can try this in your compiled code by simply binding foo as normal first:

let foo = FooImpl()
let foo : IFoo = foo    // ERROR: This expression was expected to have type 'IFoo' but here has type 'FooImpl'
let foo = foo :> IFoo   // works fine
foo.Foo()

Related Questions in F#

Related Questions in F#-INTERACTIVE