F# 向具有度量单位的记录添加扩展方法
为什么这样做有效:F# 向具有度量单位的记录添加扩展方法,f#,inline,units-of-measurement,f#-4.0,F#,Inline,Units Of Measurement,F# 4.0,为什么这样做有效: type Money = { Amount : decimal } with member inline m.gotMoney : bool = m.Amount > 0M 但事实并非如此 type MoneyUOM<[<Measure>]'currency> = { Amount : decimal<'currency> } with member inline m.gotMoney :
type Money =
{ Amount : decimal } with
member inline m.gotMoney : bool =
m.Amount > 0M
但事实并非如此
type MoneyUOM<[<Measure>]'currency> =
{ Amount : decimal<'currency> } with
member inline m.gotMoney : bool =
m.Amount > 0M<_>
type MoneyUOM}带有
成员:m.gotMoney:bool=
m、 金额>0万
相反,我得到错误FS0339:签名和实现不兼容,因为类/签名中的类型参数与成员/实现中的类型参数具有不同的编译时要求
小数WithMeasure
在这里很有用。例如,这对我很有用:
type MoneyUOM<[<Measure>]'currency> =
{ Amount : decimal<'currency> } with
member m.gotMoney() : bool =
let zero = LanguagePrimitives.DecimalWithMeasure<'currency> 0M
m.Amount > zero
type MoneyUOM}带有
成员m.gotMoney():bool=
让zero=LanguagePrimitives.decimalwithmeasureu很幸运,这是因为您使用的是decimal
而不是float
。问题是0M
不被视为通用值,而0.0
则被视为通用值。@kvb这在某种程度上回答了问题的“为什么”部分。请把它转换成一个答案,这样你就可以得到应得的投票。