練習問題 12.1 calcの定義
以下の calc の定義のおかしな点を指摘せよ。
# class calc =
object
val mutable num = 0
val mutable func = fun x -> x
method input n = num <- n
method plus = func <- (fun y -> num + y)
method eq = func num
end;;
class calc :
object
val mutable func : int -> int
val mutable num : int
method eq : int
method input : int -> unit
method plus : unit
end
解答
class calc =
object (s)
val mutable num = 0
val mutable func = fun x -> x
method input n = num <- n
method plus =
let x = num in
func <- (fun y -> x + y);
method eq = func num
end;;
let my = new calc;;
my#input 11; my#plus; my#input 23; my#eq;;
num を x に入れておかないと、次の input メソッドで新しい値が num にセットされてしまう。