gpt4 book ai didi

f# - 向带有度量单位的记录添加扩展方法

转载 作者:行者123 更新时间:2023-12-04 13:42:32 28 4
gpt4 key购买 nike

为什么这样做:

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<_>

相反,我得到 error FS0339: The signature and implementation are not compatible because the type parameter in the class/signature has a different compile-time requirement to the one in the member/implementation

最佳答案

DecimalWithMeasure在这里很有用。例如,这对我有用:

type MoneyUOM<[<Measure>]'currency> = 
{ Amount : decimal<'currency> } with

member m.gotMoney() : bool =
let zero = LanguagePrimitives.DecimalWithMeasure<'currency> 0M
m.Amount > zero

关于f# - 向带有度量单位的记录添加扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39439327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com