F# Units of measure, problems with genericity

前端 未结 2 1063
名媛妹妹
名媛妹妹 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 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

    [] 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>) = Seq.map (float >> someFloatFn) x
    let testList = [ 1 .. 4 ] |> List.map float |> List.map ((*) 1.0)
    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, ...

提交回复
热议问题