Overloading measure operator (*)

前端 未结 1 1730
既然无缘
既然无缘 2020-12-19 16:53

I am stuck attempting to overload the (*) operator for a Measure type.

What I would like to see is :

> let x = 1.0 * 1.0;;

val          


        
相关标签:
1条回答
  • 2020-12-19 17:30

    Note that you are redefining the (*) operator rather than overloading it.

    The trick to get it working is to write something using an intermediate type, like this:

    type Mult = Mult with
        static member        ($) (Mult, v1: float<i>) = fun (v2: float<i>) -> 
                                     float(-1) * float(v1) * float(v2)
        static member inline ($) (Mult, v1      ) = fun v2 -> v1 * v2
        static member        ($) (Mult, v1: Mult) = fun () -> Mult //Dummy overload
    
    let inline (*) v1 v2 = (Mult $ v1) v2
    

    BTW funny way to use units of measure.

    0 讨论(0)
提交回复
热议问题