Class 使用ocaml的方法中的可选参数

Class 使用ocaml的方法中的可选参数,class,methods,ocaml,optional-arguments,Class,Methods,Ocaml,Optional Arguments,我在方法类中遇到了一个可选参数的问题 让我解释一下。我有一个寻路类graph(在Wally模块中)和一个his方法shorthestPath。它使用可选参数。事实上,当我调用(带或不带可选参数)此方法时,OCaml返回类型冲突: Error: This expression has type Wally.graph but an expression was expected of type < getCoor : string -> int * int;


我在方法类中遇到了一个可选参数的问题

让我解释一下。我有一个寻路类
graph
(在Wally模块中)和一个his方法
shorthestPath
。它使用可选参数。事实上,当我调用(带或不带可选参数)此方法时,OCaml返回类型冲突:

Error: This expression has type Wally.graph
   but an expression was expected of type
     < getCoor : string -> int * int;
       getNearestNode : int * int -> string;
       shorthestPath : src:string -> string -> string list; .. >
   Types for method shorthestPath are incompatible
我尝试将选项格式用于可选参数:

方法短路径?src dst=
让source=将src与
|无->当前节点
|某些节点->节点
在里面
...
只有在我删除可选参数的情况下,OCaml才会停止侮辱我


提前感谢您的帮助:)

您的情况不太清楚,但我猜如下:

let f o = o#m 1 + 2

let o = object method m ?l x = match l with Some y -> x + y | None -> x

let () = print_int (f o)   (* type error. Types for method x are incompatible. *)
使用站点(这里是
f
的定义),对象的类型是从其上下文推断出来的。这里,
o:int;。>。这里固定了
x
方法的类型

后面定义的对象
o
独立于
f
的参数,其类型为
int->int;。>。不幸的是,这种类型与另一种类型不兼容

解决方法是为使用站点提供更多关于可选参数的键入上下文:

let f o = o#m ?l:None 1 + 2  (* Explicitly telling there is l *)
let o = object method m ?l x = match l with Some y -> x + y | None -> x end
或者给出
o
的类型:

class c = object
    method m ?l x = ...
    ...
end

let f (o : #c) = o#m 1 + 2   (* Not (o : c) but (o : #c) to get the function more polymoprhic *)
let o = new c
let () = print_int (f o)
我认为这更容易,因为通常事先有一个类声明

带可选参数的函数的高阶使用之间的这种小故障也发生在对象之外。OCaml试图很好地解决这个问题,但并不总是可行的。在这种情况下:

let f g = g 1 + 2
let g ?l x = match l with Some y -> x + y | None -> x
let () = print_int (f g)
他打字很好。很好


关键规则:如果OCaml无法推断省略的可选参数,请尝试显式地给出它们的类型上下文。

您的情况不太清楚,但我猜如下:

let f o = o#m 1 + 2

let o = object method m ?l x = match l with Some y -> x + y | None -> x

let () = print_int (f o)   (* type error. Types for method x are incompatible. *)
使用站点(这里是
f
的定义),对象的类型是从其上下文推断出来的。这里,
o:int;。>。这里固定了
x
方法的类型

后面定义的对象
o
独立于
f
的参数,其类型为
int->int;。>。不幸的是,这种类型与另一种类型不兼容

解决方法是为使用站点提供更多关于可选参数的键入上下文:

let f o = o#m ?l:None 1 + 2  (* Explicitly telling there is l *)
let o = object method m ?l x = match l with Some y -> x + y | None -> x end
或者给出
o
的类型:

class c = object
    method m ?l x = ...
    ...
end

let f (o : #c) = o#m 1 + 2   (* Not (o : c) but (o : #c) to get the function more polymoprhic *)
let o = new c
let () = print_int (f o)
我认为这更容易,因为通常事先有一个类声明

带可选参数的函数的高阶使用之间的这种小故障也发生在对象之外。OCaml试图很好地解决这个问题,但并不总是可行的。在这种情况下:

let f g = g 1 + 2
let g ?l x = match l with Some y -> x + y | None -> x
let () = print_int (f g)
他打字很好。很好


关键规则:如果OCaml无法推断省略的可选参数,请尝试显式地给出它们的类型上下文。

如何调用该方法?是的。例如:
let path=self#getNodes#shorthestPath~src:“begin”节点位于…
。方法
self#getNodes
获取Graph的一个实例。您如何调用该方法?是的。例如:
let path=self#getNodes#shorthestPath~src:“begin”节点位于…
。方法
self#getNodes
获取Graph的实例。显式上下文。。。实际上,我认为最好的方法是为每个模块使用接口。然而,我的项目太超前了,我没有太多的时间。但是现在,我的变量类型可能很适用。因此,使用这段代码我没有问题:
let path=self#getNodes#shorthestPath node in…
with
method private getNodes=match nodes with Some n->(n:Wally.graph)| None->failwith“寻路未初始化”
非常感谢您的帮助。很抱歉我不够清楚。明确的上下文。。。实际上,我认为最好的方法是为每个模块使用接口。然而,我的项目太超前了,我没有太多的时间。但是现在,我的变量类型可能很适用。因此,使用这段代码我没有问题:
let path=self#getNodes#shorthestPath node in…
with
method private getNodes=match nodes with Some n->(n:Wally.graph)| None->failwith“寻路未初始化”
非常感谢您的帮助。很抱歉,我不清楚。