将扩展方法添加到带有测量单位的记录

Gre*_*egC 4 f# inline units-of-measurement f#-4.0

为什么这有效:

type Money = 
   { Amount : decimal } with

   member inline m.gotMoney : bool =
      m.Amount > 0M
Run Code Online (Sandbox Code Playgroud)

但这并不

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

   member inline m.gotMoney : bool =
      m.Amount > 0M<_>
Run Code Online (Sandbox Code Playgroud)

相反,我得到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

Gru*_*oon 5

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

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

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