inheritanceconstructorf#constructor-overloading

F# Add multiple constructor overloads that call the base class constructor


I'm trying to create a dictionary with structural equality in F# and I want to pass through several dictionary constructor overloads.

In C# this would look like

class StructuralDictionary<Key, Value> : Dictionary<Key, Value> where Key : IEquatable<Key>
{
    public StructuralDictionary() : base() { }
    public StructuralDictionary(IDictionary<Key, Value> dict) : base(dict) { }
    public StructuralDictionary(IEqualityComparer<Key> keyComparer) : base(keyComparer) { }
}

The F# docs have section demonstrating this usecase, but I can't get it to compile

type StructuralDictionary<'key, 'value when 'key:equality> =

    new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
    new () = { inherit Dictionary<'key, 'value> () }

Solution

  • Ah. I was missing a inherit at the top

    type StructuralDictionary<'key, 'value when 'key:equality> =
        inherit Dictionary<'key, 'value> // <--- This has to be here
    
        new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
        new () = { inherit Dictionary<'key, 'value> () }
    

    I got a bit confused because I started with a primary constructor like

    type StructuralDictionary<'key, 'value when 'key:equality> () =
        inherit Dictionary<'key, 'value> ()
    
        new (dictionary:IDictionary<'key,'value>) = { inherit Dictionary<'key,'value>(dictionary) }
    
    

    But that doesn't work. So I tried to move the primary constructor down, but also moved the inheritance declaration so it still didn't compile.

    It turns out to overload constructors you must have inherits at the top of the type declaration but you must not specify a primary constructor at the top.