【发布时间】:2022-01-05 18:37:25
【问题描述】:
为了从构造类型tainted_value映射到其他类型,从其他基本类型映射到构造类型tainted_value,构造了两个函数。
首先,类型tainted_value定义为:
type object_ = int
and location = Obj of object_ | Null
and closure = var * cmd * stack
and value = Fld of string | Int of int | Loc of location | Clo of closure
and tainted_value = Val of value | Error
如果我只是让我的第一个函数从 tainted_value 映射到 string 看起来像:
let tva_to_string tva1 = match tva1 with
| Val (Fld e) -> e
| _ -> None
它报告错误为:
This expression has type 'a option but an expression was expected of type string
但是,如果我将None 更改为failwith "Empty",它不会返回错误:
let tva_to_string tva1 = match tva1 with
| Val (Fld e) -> e
| _ -> failwith "Empty"
为什么?
【问题讨论】:
标签: ocaml