OCaml如何将多态数转换为浮点数?

问题描述 投票:0回答:1

我想计算一个多态数。我知道float_of_int但我想将未知类型(int或float)转换为float。那么如何将多态数转换为浮点数呢?

polymorphism ocaml
1个回答
0
投票

通过对您的用例做一些假设:

type i_or_f = Int of int | Float of float;;
let conv x = match x with Int i -> float_of_int i | Float f -> f;;

i_or_f是多态类型,可以是int或float。

 utop # conv (Int 4);;
 - : float = 4.
 utop # conv (Float 4.);;
 - : float = 4.

这是你在想什么?

© www.soinside.com 2019 - 2024. All rights reserved.