与C#库进行交互时,我发现自己希望在
是否可能使用单个重载运算符在F#中近似实现这一点,并内联适当的
Nullable
结构和引用类型中都使用C#的null合并运算符。是否可能使用单个重载运算符在F#中近似实现这一点,并内联适当的
if
情况?Nullable
结构和引用类型中都使用C#的null合并运算符。if
情况?是的,可以使用在这个SO答案"Overload operator in F#"中找到的一些小技巧。
在编译时,可以内联单个运算符的正确重载,例如('a Nullable, 'a) ->'a
或('a when 'a:null, 'a) -> 'a
。甚至可以加入('a option, 'a) -> 'a
以获得更大的灵活性。
为了提供更接近C#操作符的行为,我将默认参数'a Lazy
设置为只有在原始值为null
时才调用其源代码。
示例:
let value = Something.PossiblyNullReturned()
|?? lazy new SameType()
实现:
NullCoalesce.fs [Gist]:
//https://gist.github.com/jbtule/8477768#file-nullcoalesce-fs
type NullCoalesce =
static member Coalesce(a: 'a option, b: 'a Lazy) =
match a with
| Some a -> a
| _ -> b.Value
static member Coalesce(a: 'a Nullable, b: 'a Lazy) =
if a.HasValue then a.Value
else b.Value
static member Coalesce(a: 'a when 'a:null, b: 'a Lazy) =
match a with
| null -> b.Value
| _ -> a
let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
// calling the statically inferred member
((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
let inline (|??) a b = nullCoalesceHelper<NullCoalesce, _, _, _> a b
另外,我制作了一个库,利用这个技术以及计算表达式来处理Null/Option/Nullables,名为FSharp.Interop.NullOptAble
它使用操作符|?->
。
static member Coalesce(a: 'a option ref, b: unit -> 'a) = match a.Value with Some a -> a | _ -> b()
。顺便说一下,您可以通过与null匹配来放弃等式约束。 - kaeferlet foo = nullableOfInt??? <@ 10 @>
。 - Damian//https://gist.github.com/tallpeak/7b8beacc8c273acecb5e
open System
let inline isNull value = obj.ReferenceEquals(value, null)
let inline isDBNull value = obj.ReferenceEquals(value, DBNull.Value)
type NullCoalesce =
static member Coalesce(a: 'a option, b: 'a Lazy) = match a with Some a -> a | _ -> b.Value
static member Coalesce(a: 'a Nullable, b: 'a Lazy) = if a.HasValue then a.Value else b.Value
//static member Coalesce(a: 'a when 'a:null, b: 'a Lazy) = match a with null -> b.Value | _ -> a // overridden, so removed
static member Coalesce(a: DBNull, b: 'b Lazy) = b.Value //added to support DBNull
// The following line overrides the definition for "'a when 'a:null"
static member Coalesce(a: obj, b: 'b Lazy) = if isDBNull a || isNull a then b.Value else a // support box DBNull
let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
使用方法:
let inline (|??) a b = nullCoalesceHelper<NullCoalesce, _, _, _> a b
let o = box null
let x = o |?? lazy (box 2)
let y = (DBNull.Value) |?? lazy (box 3)
let z = box (DBNull.Value) |?? lazy (box 4)
let a = None |?? lazy (box 5)
let b = box None |?? lazy (box 6)
let c = (Nullable<int>() ) |?? lazy (7)
let d = box (Nullable<int>() ) |?? lazy (box 8)
我通常使用defaultArg
来完成这个任务,因为它是语言内置的。
let (|?) = defaultArg
。 - jbtulelet inline (|??) (a: 'a Nullable) b = if a.HasValue then a.Value else b
。我对F#还很陌生,所以我可能错了,但是你的建议(假设有参数)不会导致默认值总是被使用吗? - GilesdefaultArg
是一个内置函数,它的工作方式与博客文章中描述的 |? 完全相同。你所描述的可空版本具有严重的限制,只能与 Nullable<> 结构一起使用,而不能与任何可能具有 null 值的类型一起使用。但这个问题是针对一个单一运算符的,它可以用于 Options 或 Nullables 或其他变体,而不是为每个略有不同的 monad 添加 |?、|??、|??? 或另一个 ?。只需要一个合并所有的合并运算符。 - jbtule