2
  >     type XList<'T> (_collection : seq<'T>) =
            inherit List<'T> (_collection)
            member this.Add _item = if not <| this.Contains _item then base.Add _item
            new () = XList<'T> (Seq.empty<'T>);;

              inherit List<'T> (_collection)
      --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

stdin(47,9): error FS0945: Cannot inherit a sealed type


My understanding is that List<'T> is actually not sealed. No?

Also, this seems to work just fine outside F# interactive. That exact code is in my F# project, and the compiler processes it without complaining. I've got the same thing going on in a couple of C# projects. The code works as expected in every case.

Normally, I'd just extend List<'T> with a static method (doing it the "F# way"), but hiding List.Add should work fine, too.

MiloDC
  • 2,373
  • 1
  • 16
  • 25

3 Answers3

5

As others already explained, your code actually tries to inherit from the F# list type (which is sealed). This is a bit confusing, but F# provides an alias ResizeArray<T> that stands for the generic .NET List<T> type, so you can solve this without using long names too:

type XList<'T> (_collection : seq<'T>) = 
  inherit ResizeArray<'T> (_collection) 
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
4

Try fully qualifying the type name: inherit System.Collections.Generic.List<'T> (_collection)

Daniel
  • 47,404
  • 11
  • 101
  • 179
  • Thanks, I knew that S.C.G.List and F# list would bite me in the ass eventually. Broke my cherry, now. – MiloDC Jun 11 '11 at 04:50
2

I am guessing your F# interactive has a different set of open namespaces than your F# project code? That is, is this System.Collections.Generic.List or what?

Brian
  • 117,631
  • 17
  • 236
  • 300