当前位置 : 主页 > 手机开发 > 其它 >

使用F#继承null引用异常

来源:互联网 收集:自由互联 发布时间:2021-06-19
有没有办法让下面的代码工作? open System.Collections.Generictype Geometry't(child: 't) = let values = List() member o.add (v: float) = values.Add v; childand Line() as self = inherit GeometryLine(self) member o.length v = o.ad
有没有办法让下面的代码工作?

open System.Collections.Generic

type Geometry<'t>(child: 't) =
  let values = List()
  member o.add (v: float) = values.Add v; child

and Line() as self =
  inherit Geometry<Line>(self)
  member o.length v = o.add v

let line = Line().length(50.0)

我明白了

System.NullReferenceException: Object reference not set to an instance of an object.

编辑:

调用以下内容就足以触发异常.

let line = Line()

动机是你可以做到例如:

let line = Line().x1(10).y1(20).x2(30).y2(10).color("blue") // ...

你可以在所有几何中重复使用普通成员(圆形,椭圆形……)

open System.Collections.Generic

type Geometry<'t when 't :> Geometry<'t>>() =
  let values = List()
  member o.add (v: float) = values.Add v; o :?> 't

and Line() =
  inherit Geometry<Line>()
  member o.length v = o.add v

let line = Line().length(50.0)
网友评论