这是您要查找的语法:
module File1
open System
open System.Collections.Generic
type TimeRangeList<'e> =
inherit List<'e>
val tFrom: DateTime
val tTo: DateTime
new (getter: DateTime * DateTime -> List<'e>, ?maybe_tFrom: DateTime, ?maybe_tTo: DateTime) as this =
let to_ = defaultArg maybe_tTo DateTime.Now
let from_ = defaultArg maybe_tFrom (to_.AddDays(-1.0))
{
inherit List<'e>()
tTo = to_
tFrom = from_
}
then
this.AddRange(getter(this.tFrom, this.tTo))
文档链接:
稍微解释一下,
{ field = value; field2 = value2 }
语法不一定是
只有
在中找到的表达式
新建()
定义辅助构造函数的块。它必须是
最后的
表达式,即返回的表达式。(在这里,尽管技术上
然后
块是构造函数中的“最后一个”块,其返回值(需要
unit
)被忽略,构造函数的实际返回值是最后一个表达式
在中找不到
然后
块
)。因此,使用它是安全的
let
前面定义要放入类字段中的值的表达式,以及
让
表达式可以像在普通代码中那样互相引用。因此,如果您需要将一个复杂或昂贵的计算放入多个字段中,那么可以这样做:
new () =
let result = expensiveCalculationIWantToDoOnlyOnce()
{ field1 = result; field2 = result + 1; field3 = result + 2 }