Is it possible to define units of measure for kB, GB, … with explicit or implicit conversion?

安稳与你 提交于 2019-12-02 10:44:25

Are active patterns "cool" enough for you?

[<Measure>] type kB

// single case active pattern to convert from kB to raw B value
let (|Bytes|) (x : int<kB>) = int(x * 1024)

// use pattern matching in the declaration
// val printBytes : int<kB> -> unit
let printBytes (Bytes(b)) = 
    printfn "It's %d bytes" b

printBytes 7<kB>
// "It's 7168 bytes"

The short answer to your question seems to be no. Which strikes me as add because this conversion is a bit shift. Alas, how about static methods as prescribed from "Programming f# 3.0" page 108?

[<Measure>]
type B =
    static member toKB (x: int<B>) =
        1<KB> * x / 1024<B> 

and [<Measure>] KB =
    static member toB (x: int<KB>) =
        x * 1024<B> / 1<KB>

let b1 = 1024<B>
let kb1 = B.toKB b1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!