Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types 检查OCaml中某些值v的类型_Types_Polymorphism_Ocaml - Fatal编程技术网

Types 检查OCaml中某些值v的类型

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,其中v可以是int,bool,甚至是类似于string option*string的东西,我如何检查v是int,bool,还是string option*string

如果这是不可能的,那么也许我处理我的问题是错误的


根据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)。