【问题标题】:Converting OCaml to F#: How to convert type for OCaml Format module将 OCaml 转换为 F#:如何转换 OCaml 格式模块的类型
【发布时间】:2012-09-03 19:11:30
【问题描述】:

我正在将 OCaml Format module 转换为 F#;见我的earlier question

开始我改变了

字体大小

external size_of_int : int -> size = "%identity"

外部 int_of_size : 大小 -> int = "%identity"

让 size_of_int = sizeof<int>

还有一些我知道不正确的调整,但它允许我转换所有与 open_box 和 close_box 相关的代码,除了这三行。

现在我只需要更改这三行,以便我可以测试我转换的格式模块的子集。

我知道带有 size_of_int 和 int_of_size 而 external 的行可能依赖于 F# 核心中的某些功能。我也知道%identity 可以忽略转换。

我最好的猜测是我只需要使用 size_of_int 和 int_of_size 创建一个名为 size 的简单类型,但是如何?

编辑

根据Jeffrey Scofield 的回答,我能够创建以下 F# 代码。

type size =
  interface
    abstract size_of_int : int -> size
    abstract int_of_size : size -> int
  end

type size = int
let size_of_int i = i
let int_of_size s = s

这使我的 Format 模块的子集能够成功编译。

编辑

Jack,下面回答的,有版本FSharpx.Compatibility.OCaml.Format.Format.fs 我没有测试过,但它是我目前找到的最完整的版本。

【问题讨论】:

    标签: types f# ocaml external


    【解决方案1】:

    这些行:

    type size
    external size_of_int : int -> size = "%identity"
    external int_of_size : size -> int = "%identity"
    

    创建一个与 int 相同的抽象类型。转换函数 是无操作(身份功能)。我不知道 F# 习语,但在 OCaml 你可以使用接口文件,避免使用“%identity”的巧妙。

    (* Interface file *)
    type size
    val size_of_int : int -> size
    val int_of_size : size -> int
    
    (* Implementation file *)
    type size = int
    
    let size_of_int i = i
    let int_of_size s = s
    

    希望这更容易转化为 F#。

    【讨论】:

      【解决方案2】:

      作为参考,您可以在 F# 中处理此代码的另一种类型更安全的方法:使用计量单位类型注释。

      当您在 F# 中编写 type size = int 时,size 只是 int 的别名——因此 F# 编译器很乐意允许您混合和匹配它们。例如:

      let someSize : size = 10
      let badSum = someSize + 3  // 'someSize' is used like another other 'int'
      

      如果您想要更多的类型安全性,您可以定义一个度量单位类型 Size 并像这样使用它:

      [<Measure>] type Size
      type size = int<Size>
      
      let inline size_of_int i =
          LanguagePrimitives.Int32WithMeasure<Size> i
      let inline int_of_size s = int s
      
      // Now, using a variable of type 'size' where an 'int' is expected
      // (or vice versa) will result in a compilation error.
      let someSize = size_of_int 10
      let badSum = someSize + 3  // compilation error here
      

      如果您是从 OCaml 转到 F#,这可能有点熟悉。 F# 中的度量单位类型在编译时被删除,但它们有助于减少代码中的简单数学错误——在 OCaml 中可以使用幻像类型来达到相同的目的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 2012-09-08
        • 2012-09-02
        • 2015-12-01
        • 1970-01-01
        相关资源
        最近更新 更多