F# Units of measure, problems with genericity

前端 未结 2 1064
名媛妹妹
名媛妹妹 2020-12-21 07:12

(I\'m still banging on with units of measure in F#)

I\'m having a problem making \'generic\' functions which take \'typed\' floats.

The following mo

相关标签:
2条回答
  • 2020-12-21 07:53

    Units of measure cannot be used as type parameters. This is because the are erased by the compiler during compilation. This question is quite similar: F# Units of measure - 'lifting' values to float<something>

    0 讨论(0)
  • 2020-12-21 08:03

    You can change the first 'compiler no like' to

    let mutable error_s : float<'a> = 0.0<_>
    

    and the compiler seems to like that.

    As for the second question, I am not seeing the same error as you, and this

    [<Measure>] type km 
    //define a unit of measure
    let someFloatFn x = x + 1.2 //this is a function which takes 'vanilla' floats
    let MapSeqToNonUnitFunction (x:seq<float<_>>) = Seq.map (float >> someFloatFn) x
    let testList = [ 1 .. 4 ] |> List.map float |> List.map ((*) 1.0<km>)
    let testList2 = testList :> seq<_>
    let result = MapSeqToNonUnitFunction testList2
    printfn "%A" result
    

    compiles for me (though the upcast to seq<_> is a little annoying, I am not sure if there is an easy way to get rid of it or not).

    Aside, I think convention is to name units parameters 'u, 'v, ... rather than 'a, 'b, ...

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