2011-06-10 73 views
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 


我的理解是,名单<“T>实际上密封。没有?F#:无法从列表中<'T> F#互动继承

此外,这似乎在F#交互式以外工作得很好。这个确切的代码在我的F#项目中,编译器无需抱怨就可以处理它。我在几个C#项目中发生了同样的事情。代码在每种情况下都按预期工作。

通常情况下,我只是用一个静态方法(做它的“F#方法”)扩展List <'T>,但隐藏List.Add也应该很好。

回答

4

尝试完全限定的类型名称:inherit System.Collections.Generic.List<'T> (_collection)

+0

谢谢,我知道SCGList和F#名单将最终咬我的屁股。现在打破我的樱桃。 – MiloDC 2011-06-11 04:50:20

2

我猜你的F#interactive有一套不同于你的F#项目代码的open命名空间?也就是说,这是System.Collections.Generic.List还是什么?

5

正如其他人已经解释过,你的代码实际上是尝试从F#列表类型(密封)继承。这是一个有点混乱,但F#提供了代表通用.NET List<T>类型的别名ResizeArray<T>,这样你就可以解决这个问题,而无需使用长的名字太:

type XList<'T> (_collection : seq<'T>) = 
    inherit ResizeArray<'T> (_collection) 
+0

好主意,谢谢! – MiloDC 2011-06-11 04:48:40