Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Oop 如果字符串类型是预期的,为什么不被接受?_Oop_Ocaml - Fatal编程技术网

Oop 如果字符串类型是预期的,为什么不被接受?

Oop 如果字符串类型是预期的,为什么不被接受?,oop,ocaml,Oop,Ocaml,我正在尝试从以下位置创建和使用堆栈对象(修改为具有字符串): 我无法理解错误:如果预期的类型是string,为什么不接受string类型的表达式 我无法理解错误:如果预期的类型是string,为什么不接受string类型的表达式 因为表达式s#pop没有类型string。它的类型是string选项,即它要么是Some s,要么是None,其中s具有类型字符串 查看pop方法实现,如果堆栈中有更多元素等待,则返回Some s;如果堆栈为空,则返回None method pop = match

我正在尝试从以下位置创建和使用堆栈对象(修改为具有字符串):

我无法理解错误:如果预期的类型是string,为什么不接受string类型的表达式

我无法理解错误:如果预期的类型是string,为什么不接受string类型的表达式

因为表达式
s#pop
没有类型
string
。它的类型是
string选项
,即它要么是
Some s
,要么是
None
,其中
s
具有类型
字符串

查看
pop
方法实现,如果堆栈中有更多元素等待,则返回
Some s
;如果堆栈为空,则返回
None

method pop =
  match v with
  | hd :: tl -> 
    v <- tl;
    Some hd  (* returns `Some hd` *)
  | [] -> None (* returns `None` *)
下面是它的用法

let () =
  s#push "first";
  s#push "second";
  s#push "third";
  print_some  s#pop; 
  print_some  s#pop;
  print_some  s#pop;
下面是
堆栈
对象的一些替代实现,它们使用其他方式向调用者传达堆栈为空,例如,来自堆栈元素域(由用户提供)的哨兵值、异常或
结果
类型,使用严格类型错误参数化

let stack_with_sentinel empty = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        hd
      | [] -> empty
  end

let stack_with_exception = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        hd
      | [] -> raise Not_found
  end

let stack_with_result = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        Ok hd
      | [] -> Error "empty stack"
  end 
让堆栈_与_sentinel empty=object
可变值v=[]
方法pop=将v与
|hd::tl->
v空
结束
让堆栈_与_异常=对象
可变值v=[]
方法pop=将v与
|hd::tl->
未找到v raise_
结束
让堆栈_与_结果=对象
可变值v=[]
方法pop=将v与
|hd::tl->
v错误“空堆栈”
结束

有许多其他的方法来定义它,但是使用
选项
类型是最常见的

为什么要使用一些?为什么不干脆
s
pop
方法能否返回堆栈中的顶部字符串?另外,如果堆栈中只有一个元素,
pop
方法会起作用吗?
pop
方法本质上是非总计的,例如,如果堆栈为空,它应该返回什么?有几个选项可用,您可以返回
选项类型的值,引发异常,或在返回值的域中指定一些前哨值并返回它,例如,您可以返回空字符串
或特殊字符串
“empty”
。这些都是不同的设计选择,在不同的场景中具有不同的适用性。没有最好的选择,但是在现代OCaml中使用选项类型是非常惯用的。如果您可以为pop fn添加代码以返回堆栈顶部的项,那将是非常棒的。
 let print_some s = match s with
   | None -> print_endline "empty"
   | Some s -> print_endline s
let () =
  s#push "first";
  s#push "second";
  s#push "third";
  print_some  s#pop; 
  print_some  s#pop;
  print_some  s#pop;
let stack_with_sentinel empty = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        hd
      | [] -> empty
  end

let stack_with_exception = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        hd
      | [] -> raise Not_found
  end

let stack_with_result = object
    val mutable v = []
    method pop = match v with
      | hd :: tl -> 
        v <- tl;
        Ok hd
      | [] -> Error "empty stack"
  end