F# 度量单位,通用性问题 [英] F# Units of measure, problems with genericity

查看:20
本文介绍了F# 度量单位,通用性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(仍然 在 F# 中使用度量单位)

我在制作采用类型化"浮点数的通用"函数时遇到问题.

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

以下模型类旨在根据因子c"密切关注位置累积误差.编译器不喜欢我在类型​​的主体中说 0.<'a>(测量单位文字中的意外类型参数").

The following mockup class is intended to keep tabs on a cumulative error in position, based on a factor 'c'. The compiler doesn't like me saying 0.<'a> in the body of the type ("Unexpected type parameter in unit-of-measure literal").

///Corrects cumulative error in position based on s and c
type Corrector(s_init:float<'a>) =
    let deltaS ds c = sin (ds / c) //incremental error function

    //mutable values
    let mutable nominal_s = s_init
    let mutable error_s = 0.<'a>  //<-- COMPILER NO LIKE

    ///Set new start pos and reset error to zero
    member sc.Reset(s) = 
        nominal_s <- s
        error_s <- 0.<'a>  //<-- COMPILER NO LIKE

    ///Pass in new pos and c to corrector, returns corrected s and current error    
    member sc.Next(s:float<'a>, c:float<'a>) = 
        let ds = s - nominal_s //distance since last request
        nominal_s <- s   //update nominal s
        error_s <- error_s + (deltaS ds c) //calculate cumulative error
        (nominal_s + error_s, error_s) //pass back tuple

<小时>

我相信,另一个相关问题仍然与通用"功能有关.


Another related question, I believe, still to do with 'generic' functions.

在下面的代码中,我想要做的是创建一个函数,该函数将采用任何类型的浮点数的 #seq 并将其应用于仅接受vanilla"浮点数的函数.第三行给出了值限制"错误,我看不到任何出路.(删除 # 可以解决问题,但我想避免为列表、序列、数组等编写相同的内容.)

In the following code, what I am trying to do is make a function which will take a #seq of any type of floats and apply it to a function which only accepts 'vanilla' floats. The third line gives a 'Value Restriction' error, and I can't see any way out. (Removing the # solves the problem, but I'd like to avoid having to write the same thing for lists, seqs, arrays etc.)

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

推荐答案

你可以把第一个'compiler no like'改成

You can change the first 'compiler no like' to

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

编译器似乎喜欢那样.

关于第二个问题,我没有看到和你一样的错误,还有这个

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

为我编译(虽然向上转换到 seq<_> 有点烦人,我不确定是否有简单的方法来摆脱它).

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).

另外,我认为约定是将单位参数命名为 'u, 'v, ... 而不是 'a, 'b, ...

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

这篇关于F# 度量单位,通用性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆