Types 检查OCaml中某些值v的类型
我有一些v,其中v可以是int,bool,甚至是类似于string option*string的东西,我如何检查v是int,bool,还是string option*string 如果这是不可能的,那么也许我处理我的问题是错误的Types 检查OCaml中某些值v的类型,types,polymorphism,ocaml,Types,Polymorphism,Ocaml,我有一些v,其中v可以是int,bool,甚至是类似于string option*string的东西,我如何检查v是int,bool,还是string option*string 如果这是不可能的,那么也许我处理我的问题是错误的 根据v是什么,我想返回相同多态类型的不同类型,比如我声明的类型“value”。如果需要,可以使用GADT: type _ value = Int : int -> int value | String : string -> string valu
根据v是什么,我想返回相同多态类型的不同类型,比如我声明的类型“value”。如果需要,可以使用GADT:
type _ value =
Int : int -> int value
| String : string -> string value
| Bool : bool -> bool value
let get_value : type v. v value -> v = function
Int i -> i
| String s -> s
| Bool b -> b
例如:
let _ =
Printf.printf "%d %s %b\n%!"
(get_value (Int 2))
(get_value (String "toto"))
(get_value (Bool false))
您可以在其上测试代码。如果需要,您可以使用GADT:
type _ value =
Int : int -> int value
| String : string -> string value
| Bool : bool -> bool value
let get_value : type v. v value -> v = function
Int i -> i
| String s -> s
| Bool b -> b
例如:
let _ =
Printf.printf "%d %s %b\n%!"
(get_value (Int 2))
(get_value (String "toto"))
(get_value (Bool false))
您可以在其上测试代码。如果需要,您可以使用GADT:
type _ value =
Int : int -> int value
| String : string -> string value
| Bool : bool -> bool value
let get_value : type v. v value -> v = function
Int i -> i
| String s -> s
| Bool b -> b
例如:
let _ =
Printf.printf "%d %s %b\n%!"
(get_value (Int 2))
(get_value (String "toto"))
(get_value (Bool false))
您可以在其上测试代码。如果需要,您可以使用GADT:
type _ value =
Int : int -> int value
| String : string -> string value
| Bool : bool -> bool value
let get_value : type v. v value -> v = function
Int i -> i
| String s -> s
| Bool b -> b
例如:
let _ =
Printf.printf "%d %s %b\n%!"
(get_value (Int 2))
(get_value (String "toto"))
(get_value (Bool false))
您可以在上面测试代码是否正常。您的类型
值
看起来如何?它有什么构造函数?如果它像type'a value=Val of'a
,你可以创建一个函数let convert_option_to_value=function | Some x->Val x | None->failwith“not supported”
。否则你可以有一个额外的参数来具体化v的类型(例如使用GADT)。你的type值是什么样的?它有什么构造函数?如果它像type'a value=Val of'a
,你可以创建一个函数let convert_option_to_value=function | Some x->Val x | None->failwith“not supported”
。否则你可以有一个额外的参数来具体化v的类型(例如使用GADT)。你的type值是什么样的?它有什么构造函数?如果它像type'a value=Val of'a
,你可以创建一个函数let convert_option_to_value=function | Some x->Val x | None->failwith“not supported”
。否则你可以有一个额外的参数来具体化v的类型(例如使用GADT)。你的type值是什么样的?它有什么构造函数?如果类似于type'a value=Val of'a
,您可以创建一个函数let convert_option_to_value=function | Some x->Val x | None->failwith“not supported”
。否则,您可以使用一个额外的参数来具体化v的类型(例如,使用GADT)。