How to inherit from an abstract class in an fsharp signature file?

196 Views Asked by At

I am trying to model a domain in FSharp. And I would like client code to experience clean access to these types. So part of it is by creating a signature file (.fsi), which is described here: https://msdn.microsoft.com/en-us/library/dd233196.aspx

The Microsoft page does not describe how inheritance works in FSharp signature files. I did discover how to indicate an interface implementation with the "interface" keyword here: https://github.com/intellifactory/websharper.ui.next/blob/master/WebSharper.UI.Next/Attr.fsi

Nice. But I wanted to take it a step further, and inherit from an abstract class, and indicate that in the signature file.

The goal is polymorphism. Somewhere, I want a function to take a parameter "v" as: (v : S list), where S is a supertype and the list may consist values of its subtypes.

But what is the syntax in an fsharp signature file for inheritance from an abstract class?

In "Artefact.fs" I have (yes, its a Websharper project):

[<JavaScript; AbstractClass>]
type Artefact() =
    abstract member Visual : Doc with get

And in "Visual.fsi" I have:

[<JavaScript; Sealed>]
type Visual = 
    inherit Artefact

The "inherit" line does not compile. So what is the right syntax?

I tried these (which also don't work):

1) brackets after Artefact

[<JavaScript; Sealed>]
type Visual = 
    inherit Artefact()

2) brackets after Visual

[<JavaScript; Sealed>]
type Visual() = 
    inherit Artefact

Any ideas?

1

There are 1 best solutions below

0
On

This is the correct syntax to inherit from an abstract class, in a signature file:

[<JavaScript; Sealed>]
type Visual = 
    class
        inherit Artefact
    end

Explanation (as I see it). In the syntax I used in my question above, the compiler does not understand that the type is a class. Not all types can inherit, so there you have the compiler's complaint.

In the syntax I use in this answer, I explicitly declare that the type is a class. I didn't know this syntax, I've only seen "class end", and I never thought that you could add stuff in between those two keywords.

Thanks to the response above. Automatically generate an .fsi file if you don't know the syntax. This will give you the syntax, which you can customize and clean-up. See this post for automatic signature file generation: Automated F# Signature File (.fsi) Generation