F# casting an object to an interface

f#, interface

Solution

F# doesn't allow inheritence in quite the way you want.

A better way would be to use:

[p1 ;p2] |> List.map (fun x -> x:> IPane)

Alternatively, you can change your function to use something like this

let f (t:#IPane list) = ()

and here you can do `f [p1;p2]` as the `#` tells the compiler that any type that inherits from `IPane` is fine.

Problem

I have a class called 'Pane' (think glass pane) that implements IPane: ``` type IPane = abstract PaneNumber : int with get, set abstract Thickness : float<m> with get, set abstract ComponentNumber : int with get, set abstract Spectra : IGlassDataValues with get, set ... type Pane(paneNumber, componentNumber, spectra, ...) = let mutable p = paneNumber let mutable n = componentNumber let mutable s = spectra ... interface IPane with member this.PaneNumber with get() = p and set(value) = p <- value member this.ComponentNumber with get() = n and set(value) = n <- value member this.Spectra with get() = s and set(value) = s <- value ... ``` I create a list of panes (Pane list): ``` let p = [ p1; p2 ] ``` however I need to cast this to an IPane list as this is a parameter type in another function. The following code produces an error: ``` let p = [ p1; p2 ] :> IPane list 'Type constraint mismatch. The type Pane list is not compatible with type IPane list The type 'IPane' does not match the type 'Pane' ``` This is confusing as Pane implements IPane. Simply passing the Pane list object as a parameter into the required function also produces an error. How do I cast Pane list to IPane list?

Original source