F# 什么时候不应该使用度量单位?

F# 什么时候不应该使用度量单位?,f#,units-of-measurement,F#,Units Of Measurement,什么时候不应该使用度量单位? 我觉得计算中使用的任何值都应该应用一个度量单位 我的想法准确吗? 如果是,那么为什么F#在默认情况下不强制执行此约束以鼓励正确性? 在我的例子中,我在篮球比赛中使用了度量单位: 例如: [<Measure>] type pts type Player = { Score:int<pts> } []类型pts 类型Player={Score:int} 以下是利用度量单位的模型: (*Types*) [<Measure>] typ

什么时候不应该使用度量单位?

我觉得计算中使用的任何值都应该应用一个度量单位

我的想法准确吗?

如果是,那么为什么F#在默认情况下不强制执行此约束以鼓励正确性?

在我的例子中,我在篮球比赛中使用了度量单位:

例如:

[<Measure>] type pts
type Player = { Score:int<pts> }
[]类型pts
类型Player={Score:int}
以下是利用度量单位的模型:

(*Types*)
[<Measure>] type pts
type Player = { Score:int<pts> }

type FieldShot = TwoPointer| ThreePointer
type FoulShots = FoulShot  | TwoFoulShots | ThreeFoulShots

type FoulShooter  = FoulShooter  of Player
type FieldShooter = FieldShooter of Player

(*Functions*)
let shoot (lastShot:int<pts>) player =
    (player.Score + lastShot)

let fieldShot (fieldShooter, shot) =

    let player = match fieldShooter with
                 | FieldShooter player -> player

    match player.Score with
    | score when score > 10<pts> -> score
    | _ ->  match (fieldShooter, shot) with
            | FieldShooter player, shot -> match shot with
                                           | TwoPointer   -> player |> shoot 2<pts>
                                           | ThreePointer -> player |> shoot 3<pts>
let foulShot (foulShooter, shot) =

    let player = match foulShooter with
                 | FoulShooter player -> player

    match player.Score with
    | score when score >= 11<pts> -> score
    | _ ->  match (foulShooter, shot) with
            | FoulShooter player, shot -> match shot with
                                          | FoulShot       -> player |> shoot 1<pts>
                                          | TwoFoulShots   -> player |> shoot 2<pts>
                                          | ThreeFoulShots -> player |> shoot 3<pts>

let makeFoulShots foulShots (shooter, defender) = 
    FieldShooter { Score= foulShot (shooter, foulShots) }, defender

let makeFieldShot fieldBasket (shooter, defender) =
    FoulShooter { Score= fieldShot (shooter, fieldBasket) }, defender

let turnover (shooter, defender) = (defender, shooter)

(*Client*)
let player1, player2 = FieldShooter { Score=0<pts> } ,
                       FieldShooter { Score=0<pts> }

let results = (player1, player2) |> makeFieldShot TwoPointer
                                 |> makeFoulShots ThreeFoulShots
                                 |> makeFieldShot TwoPointer
                                 |> makeFoulShots TwoFoulShots
                                 |> makeFieldShot TwoPointer
                                 |> makeFoulShots FoulShot
(*类型*)
[]型临时秘书处
类型Player={Score:int}
类型FieldShot=TwoPointer | ThreePointer
类型FoulShots=FoulShot |两个FoulShots |三个FoulShots
类型FoulShooter=玩家的FoulShooter
类型FieldShooter=球员的FieldShooter
(*功能*)
让我们射击(最后一杆:int)球员=
(player.Score+最后一击)
让外野射击(外野射击手,射击)=
让玩家=将野战射手与
|野战射手球员->球员
比赛选手。用球得分
|得分>10时得分->得分
|->与(野战射击手,射击)匹配
|投篮手球员,投篮->配合投篮
|二分球->球员|>投篮2
|三分球->球员|>投篮3
让犯规射门(犯规射门,射门)=
让玩家=与犯规者比赛
|FoulShooter玩家->玩家
比赛选手。用球得分
|得分>=11->得分时得分
|_u->将(犯规、射门)与
|FoulShooter玩家,投篮->配合投篮
|犯规->球员|>投篮1
|两个罚球->球员|>投篮2
|三次犯规->球员|>投篮3次
让我们把球拍成球拍(射手、后卫)=
投篮手{Score=foulShot(投篮手,foulShot)},防守队员
让makeFieldShot投篮篮(射手、后卫)=
犯规投手{得分=投篮(投手,篮筐)},防守队员
让营业额(射手,防守队员)=(防守队员,射手)
(*客户*)
让player1,player2=FieldShooter{Score=0},
投篮手{得分=0}
让结果=(player1,player2)|>makeFieldShot二分球
|>拍三张犯规照
|>二分球
|>制作两张foulshot
|>二分球
|>使FoulShot成为FoulShot

嗯,我想到的唯一答案是,通常不应该使用度量单位

考虑pi或e等值

无量纲值通常由具有某些物理维度的量的比率产生

假设您想创建一些程序逻辑,当一个队的篮球得分达到另一个队得分的两倍时触发,您需要一个无量纲数量:

if teamBScore >= 2 * teamAScore then
    ...
else
    ...
2
必须在计算中使用,并且必须是无量纲的,否则
teamAScore
teamBScore
的单位将不匹配

当然,严格地说,您可以用
1
的单位显式地注释这些值,但这可能有些过分


除此之外,问题的其余部分主要是基于观点的。您应该使用或不使用度量单位,这取决于您如何评估您将以(略微)增加的编程时间和冗长性为代价获得的安全性增益。

嗯,唯一的答案是,您通常不应该使用度量单位

考虑pi或e等值

无量纲值通常由具有某些物理维度的量的比率产生

假设您想创建一些程序逻辑,当一个队的篮球得分达到另一个队得分的两倍时触发,您需要一个无量纲数量:

if teamBScore >= 2 * teamAScore then
    ...
else
    ...
2
必须在计算中使用,并且必须是无量纲的,否则
teamAScore
teamBScore
的单位将不匹配

当然,严格地说,您可以用
1
的单位显式地注释这些值,但这可能有些过分

除此之外,问题的其余部分主要是基于观点的。您应该使用或不使用度量单位,这取决于您如何评估您将以(略微)增加编程时间和冗长为代价获得的安全性增益

如果是的话,那么为什么F#在默认情况下不强制执行这个约束来鼓励正确性呢

首先,因为它需要与不支持度量单位的语言进行互操作。如果您想在计算中使用来自C#library方法的值,它如何决定使用哪种单位?即使F#开发人员花费大量时间用度量单位注释所有标准库方法,这对第三方库也没有帮助

如果是的话,那么为什么F#在默认情况下不强制执行这个约束来鼓励正确性呢


首先,因为它需要与不支持度量单位的语言进行互操作。如果您想在计算中使用来自C#library方法的值,它如何决定使用哪种单位?即使F#开发人员花费大量时间用度量单位注释所有标准库方法,这对第三方库也没有帮助。

我相信在您的案例中,使用UOM不会给程序带来任何有用的东西,因为您不会使用不同的单位进行计算。如果您在和中有表示长度的值,那么它可能会很有用。当数字有一些附加元数据(例如屏幕/图纸坐标)时,UOM可能会很有用(模拟之外)。一件事可能是区分点sc