向具有计量单位的记录添加扩展方法

Adding an extension method to a record with unit of measure

为什么这样做:

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