它没有方法 the_method
It has no method the_method
假设liste
是某个对象列表。
let rec sum liste any_method = match liste with
| [] -> 0
| obj::r -> obj#any_methode + sum r any_method
如果我使用 sum my_list_of_objects the_method
,我会得到 This expression has type int list. It has no method the_method
。
是否有解决该问题的方法?
更新
这是一个简单的案例
class point =
object
val mutable x = 1
method getx = x
method move d = (x <- x + d)
end;;
假设liste_points
对象列表point
。
let func liste methode = map (fun obj -> obj#methode 5) liste;;
func liste_points move;;
上面的小代码出现了下面的错误:
Error: This expression has type point list
but an expression was expected of type
(< methode : int -> 'b; .. > as 'a) list
Type point = < getx : int; move : int -> unit >
is not compatible with type < methode : int -> 'b; .. > as 'a
The first object type has no method methode
我怎样才能完成这项工作?
新答案
这里有一个 class 实例包含两个不同的整数:
class intx2 = object
val mutable a : int = 0
val mutable b : int = 0
method set_a v = a <- v
method get_a = a
method set_b v = b <- v
method get_b = b
end
let get_a_fn obj = obj#get_a
let get_b_fn obj = obj#get_b
let rec sum getter = function
| [] -> 0
| obj :: rest -> getter obj + sum getter rest
您可以像这样总结此类对象列表的 a
值:
sum get_a_fn list_of_objs
您可以这样总结 b
个值:
sum get_b_fn list_of_objs
假设liste
是某个对象列表。
let rec sum liste any_method = match liste with
| [] -> 0
| obj::r -> obj#any_methode + sum r any_method
如果我使用 sum my_list_of_objects the_method
,我会得到 This expression has type int list. It has no method the_method
。
是否有解决该问题的方法?
更新 这是一个简单的案例
class point =
object
val mutable x = 1
method getx = x
method move d = (x <- x + d)
end;;
假设liste_points
对象列表point
。
let func liste methode = map (fun obj -> obj#methode 5) liste;;
func liste_points move;;
上面的小代码出现了下面的错误:
Error: This expression has type point list
but an expression was expected of type
(< methode : int -> 'b; .. > as 'a) list
Type point = < getx : int; move : int -> unit >
is not compatible with type < methode : int -> 'b; .. > as 'a
The first object type has no method methode
我怎样才能完成这项工作?
新答案
这里有一个 class 实例包含两个不同的整数:
class intx2 = object
val mutable a : int = 0
val mutable b : int = 0
method set_a v = a <- v
method get_a = a
method set_b v = b <- v
method get_b = b
end
let get_a_fn obj = obj#get_a
let get_b_fn obj = obj#get_b
let rec sum getter = function
| [] -> 0
| obj :: rest -> getter obj + sum getter rest
您可以像这样总结此类对象列表的 a
值:
sum get_a_fn list_of_objs
您可以这样总结 b
个值:
sum get_b_fn list_of_objs