F# 从构造函数中进行泛型提取
在F#和OCaml中,我最终编写了很多代码,比如F# 从构造函数中进行泛型提取,f#,pattern-matching,F#,Pattern Matching,在F#和OCaml中,我最终编写了很多代码,比如 type C = Blah of Whatever let d = Blah (createWhatever ()) // so d is type C ... let x = match d with | Blah b -> b 我想要的是这个 ... let x = peel d peel将为任何构造函数/鉴别器工作。 当然,我不是唯一一个为此烦恼的人。 编辑: 答案不错,但我没有代表投票。 这种情况怎么样 mem
type C = Blah of Whatever
let d = Blah (createWhatever ()) // so d is type C
...
let x = match d with | Blah b -> b
我想要的是这个
...
let x = peel d
peel将为任何构造函数/鉴别器工作。当然,我不是唯一一个为此烦恼的人。
编辑: 答案不错,但我没有代表投票。 这种情况怎么样
member self.Length = match self with | L lab -> lab.Length
适用于DU…需要调整才能使用类构造函数:
open Microsoft.FSharp.Reflection
let peel d =
if obj.ReferenceEquals(d, null) then nullArg "d"
let ty = d.GetType()
if FSharpType.IsUnion(ty) then
match FSharpValue.GetUnionFields(d, ty) with
| _, [| value |] -> unbox value
| _ -> failwith "more than one field"
else failwith "not a union type"
顺便说一句:我通常不会这样做,但既然你问…适用于DU…需要调整才能使用类构造函数:
open Microsoft.FSharp.Reflection
let peel d =
if obj.ReferenceEquals(d, null) then nullArg "d"
let ty = d.GetType()
if FSharpType.IsUnion(ty) then
match FSharpValue.GetUnionFields(d, ty) with
| _, [| value |] -> unbox value
| _ -> failwith "more than one field"
else failwith "not a union type"
顺便说一句:我通常不会做这样的事情,但既然你问…就不可能安全地做到这一点:如果
peel
是一个函数,它的类型是什么?它不能被输入,因此不能成为语言中的“好人”
你可以:
- 使用反射(在F#中)或类型中断函数(在OCaml中,它是
模块),但如果类型不精确,则会出现不安全的情况,因此它相当难看,而且“使用风险自负”Obj
- 使用元编程为您的每种类型生成不同版本的
。例如,使用OCaml工具,您可以隐式地定义函数peel
,以及peel_blah
type foo=foo定义
peel_foo
peel
。我认为有两种可能性:
- 您可以使用聪明的模式而不是函数:通过使用
,或let(Blah whatever)=fx
,您不再需要解包函数fun(Blah whatever)->……
- 或者,您可以不写
类型blah=blah,而是写
这样,您就没有求和类型,而是乘积类型(您编写键入blah=(blah_标记*无论什么)和blah_标记=blah
),而您的(废话,随便什么)
就是peel
。对于每个snd
、blah
等,您仍然有不同的(不兼容)类型,但有一个统一的访问接口foo
- 不可能安全地做到这一点:如果
peel
是一个函数,它的类型是什么?它不能被输入,因此不能成为语言中的“好人”
你可以:
- 使用反射(在F#中)或类型中断函数(在OCaml中,它是
模块),但如果类型不精确,则会出现不安全的情况,因此它相当难看,而且“使用风险自负”Obj
- 使用元编程为您的每种类型生成不同版本的
。例如,使用OCaml工具,您可以隐式地定义函数peel
,以及peel_blah
type foo=foo定义
peel_foo
peel
。我认为有两种可能性:
- 您可以使用聪明的模式而不是函数:通过使用
,或let(Blah whatever)=fx
,您不再需要解包函数fun(Blah whatever)->……
- 或者,您可以不写
类型blah=blah,而是写
这样,您就没有求和类型,而是乘积类型(您编写键入blah=(blah_标记*无论什么)和blah_标记=blah
),而您的(废话,随便什么)
就是peel
。对于每个snd
、blah
等,您仍然有不同的(不兼容)类型,但有一个统一的访问接口foo
- 我会写以下内容:
type C = Blah of Whatever
let d = Blah (createWhatever ()) // so d is type C
...
let (Blah x) = d
对于您的第二种情况,我喜欢Laurent的
成员x。Value=match x with Blah v->v
我将写以下内容:
type C = Blah of Whatever
let d = Blah (createWhatever ()) // so d is type C
...
let (Blah x) = d
对于第二种情况,我喜欢Laurent的
成员x。Value=match x with Blah v->v
如上所述,let
可以方便地进行模式匹配。
如果您想访问表达式中间的值,不允许使用模式,我建议将类型添加到:
type C = Blah of int
with member c.Value = match c with Blah x -> x
let x = Blah 5
let y = Blah 2
let sum = x.Value + y.Value
如前所述,
let
便于进行模式匹配。
如果您想访问表达式中间的值,不允许使用模式,我建议将类型添加到:
type C = Blah of int
with member c.Value = match c with Blah x -> x
let x = Blah 5
let y = Blah 2
let sum = x.Value + y.Value
第一次发布时没有,但现在缩进四个空格就行了。奇怪。第一次发布时没有,但现在缩进四个空格就行了。奇怪。Re:你的第一句话,它的类型应该是
'a->'b
当然了!:-)我不知道你可以做let(胡说八道)=
,但它很管用!回复:你的第一句话,它的类型应该是'a->'b
当然了!:-)我不知道你可以做let(胡说八道)=
,但它很管用!