2
我想建立一个通用的函数操作记录我的代码如下所示:F#泛型记录
type Status = Active | Inactive
type IStatus =
abstract member Status: Status
type User =
{
Username : string;
Status : Status
}
interface IStatus with
member x.Status = x.Status
let ChangeStatus<'T when 'T :> IStatus> newStatus (t:'T) =
{t with Status = newStatus}
现在我得到以下错误:
expression was expected to have type
'T
but here has type
User
很显然,我只是想创建实现IStatus的记录的类型约束。我在想OO吗?或者是否有这种方法的优点,以及如何创建ChangeStatus
函数?
谢谢您的阅读。
一旦你开始嵌套记录,并需要更新内部,它可以是值得看镜头。参见:http://bugsquash.blogspot.co.uk/2011/11/lenses-in-f.html和http://stackoverflow.com/questions/8179485/updating-nested-immutable-data-structures – TheInnerLight
@TheInnerLight我同意。我不想让答案复杂化,但请注意,该文章中描述的镜头不是多形的。实际上[FSharpPlus](https://github.com/gmpl/FSharpPlus)中有一个多态镜头模块,如果设计合理,可能值得使用。 – Gustavo
谢谢你的答案。我将需要一个周末,一个黑暗的房间和一支蜡烛来包围我的头。其中之一当然是所有“额外属性”组合的“笛卡尔乘积”。 –