Home > Back-end >  Why does "override val" not exist in f#?
Why does "override val" not exist in f#?

Time:09-19

Why is there no such thing as "override val" in F#? In the language spec I see it on page 155 https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf

But it doesn't look like it made it into the syntax: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/inheritance

CodePudding user response:

There is a thing called override val in F#, it did make it into the language. The example on page 155 compiles:

type MyBase () =
    abstract Property : string with get, set
    default val Property = "default" with get, set
type MyDerived() =
    inherit MyBase()
    override val Property = "derived" with get, set

Typing this into FSI shows that the overridden val is properly accessed and returned:

> type MyBase =
  new: unit -> MyBase
  abstract Property: string
  override Property: string
type MyDerived =
  inherit MyBase
  new: unit -> MyDerived
  override Property: string

> MyDerived().Property;;
val it: string = "derived"

I agree though, that the docs should also mention it. At best you could say it is said implicitly, where (under "Interfaces") it says that you can use val or members with override or default. This should certainly be improved.

  • Related