
-----------------------------------
wtd
Sat Dec 09, 2006 6:55 pm

[TYS] O'Caml TYS for the day
-----------------------------------
Consider the following snippet from the toplevel interpreter.

# class foo = object method baz = 42 end
  class bar = object method baz = 27 end
  
  let qux (wooble : foo) =
     print_int wooble#baz;
     print_newline ();;
class foo : object method baz : int end
class bar : object method baz : int end
val qux : foo -> unit = 

Just reasoning about this with your brain, what is the smallest change necessary such that an object of class bar can be passed to the function qux?

-----------------------------------
Null
Wed Dec 20, 2006 4:14 pm


-----------------------------------
I'm pretty sure this:


let qux (wooble : foo) =
     print_int wooble#baz;
     print_newline ();; 


becomes this:

let qux wooble =
     print_int wooble#baz;
     print_newline ();; 


Because O'Caml will realize that the method will work on any object with a baz method.

-----------------------------------
wtd
Wed Dec 20, 2006 8:16 pm


-----------------------------------
Not quite.  :)

-----------------------------------
richcash
Fri May 25, 2007 6:20 pm

Re: [TYS] O'Caml TYS for the day
-----------------------------------
I know this is old, but I'm bored. ;)


No change is necessary for objects of class bar to be passed to qux.

class foo = object method baz = 42 end
class bar = object method baz = 27 end
  
let qux (wooble : foo) =
	print_int wooble#baz;
	print_newline ();;

let f = new foo;;
let b = new bar;;

qux f;;
qux b;;

The above code compiles and runs as expected.

The type for the qux function is :
 foo -> unit
But the compiler simplifies this by replacing the type of foo in automatically :
(object baz -> int end) -> unit
It just so happens that objects of class bar have the exact same type signature as objects of foo, so you can use bar objects as well.

If bar still has the same method baz but we change its type by redifining it, then qux will no longer be able to take bar objects :
class foo = object method baz = 42 end
class bar = object method baz = 27; method fizz = 35 end

let qux (wooble : foo) =
	print_int wooble#baz;
	print_newline ();;

let f = new foo;;
let b = new bar;;

qux f;;
qux b;;
The above will cause an error on the last line.

-----------------------------------
wtd
Sat May 26, 2007 12:09 am

RE:[TYS] O\'Caml TYS for the day
-----------------------------------
Good.  Now, with your second bit of code, what would one have to change about the method signature to make it work with both classes?

Or, put another way, what should the type of wooble be?

-----------------------------------
richcash
Sat May 26, 2007 4:15 pm

Re: [TYS] O'Caml TYS for the day
-----------------------------------
Well, we can cheat and let o'caml's type inferencing figure out the type of wooble for us (not give wooble a type at all). Or, we can do it the safe way :

class foo = object method baz = 42 end
class bar = object method baz = 27; method fizz = 35 end

let qux (wooble : < baz : int; .. >) =
        print_int wooble#baz;
        print_newline ();;

let f = new foo;;
let b = new bar;;

qux f;;
qux b;;

We give wooble the special type signature < baz : int; .. > which means allow any object that has a method (baz : int), regardless of what other methods the object might have. :)

-----------------------------------
wtd
Sun May 27, 2007 1:29 am

RE:[TYS] O\'Caml TYS for the day
-----------------------------------
Alternatively:

let qux wooble = Printf.printf "%d\n" wooble#baz

-----------------------------------
richcash
Sun May 27, 2007 2:07 am

Re: [TYS] O'Caml TYS for the day
-----------------------------------
Yep, that would be a good alternative. But I think you broke your own rules there by completely redefining qux. :P

-----------------------------------
wtd
Sun May 27, 2007 2:41 am

RE:[TYS] O\'Caml TYS for the day
-----------------------------------
I made it better!  :)
