Is it possible for an F # type to "inherit" from a C # class?

I used Haskell a bit in my days, but it was a long time ago, so I decided to take a look at F #. I do a lot of daily development using C, C ++ and C #.

I noticed that when compiling F # in a dll, you can detect F # types as classes in C #. But then I thought. I have several C # classes in which I would like to put the logic in F # (to try it). But many of the existing C # codes process a lot of objects according to their base class (which is very rudimentary). Is it possible to use this base class from C # as a base for F # types? Or can F # inherit only from other types of F #?

The reason for this would be to save the base class as part of the main C # project, and specific DLL files (like similar plugins based on this contract as the base class) could be written in F #. If this is not possible, I should add another F # project containing only the base class, which feels a little more complicated.

+5
source share
2 answers

Yes. It is possible.

type SomeClass =
    inherit SomeCSharpBase

Here are some more details:

Inheritance (F #)

+11
source

F # classes can inherit .NET classes:

type FSharpClass() =
    inherit System.AccessViolationException()

Some other types of F #, such as records, discriminatory associations, cannot inherit classes, but they can implement interfaces:

type FSharpRecord =
    { d : System.IDisposable }
    interface System.IDisposable with
        member this.Dispose() = this.d.Dispose()
+5
source

All Articles