1

使用给定的 Single-Type-Case DU 进行设计

type DoB = | DoB of System.DateTime

let DoB (dt:DateTime) = // shadow constructor
    if(dt.Year>1914) // business defines what a min Date of Birth should be
    then Some(DoB dt) 
    else None

我通过 Answers to wrapping Multi-case DU's.fsi看到你可以很容易地在没有文件的情况下进行构造函数阴影。

通过展开我的意思是从文章中(如果我们有一个文件let value (EmailAddress e):string = e,它必须在.fsi文件中或应用于这种DoB类型看起来像 。let value (DoB dob):DateTime = dob.fsi

你能以某种方式对构造函数进行的解包做同样的事情吗?

我可以以某种方式定义某种解包器或增强器吗?

4

1 回答 1

4

如果我理解展开的意思,您可以使用活动模式:

//File1.fs
let (|DoB|) (DoB dt) = dt

//File2.fs
let dob = DoB DateTime.Now //using `DoB` "constructor" function
match dob with
| Some (DoB dt) -> //using `DoB` active pattern
    printfn "DOB: %A" dt 
| None -> 
    printfn "Not a DOB"

这是可行的,不会影响同名的功能,因为活动模式存在于单独的命名范围中。

于 2014-06-18T19:19:23.417 回答