Computer Science Canada

[TYS] O'Caml TYS for the day

Author:  wtd [ Sat Dec 09, 2006 6:55 pm ]
Post subject:  [TYS] O'Caml TYS for the day

Consider the following snippet from the toplevel interpreter.

code:
# 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 = <fun>


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?

Author:  Null [ Wed Dec 20, 2006 4:14 pm ]
Post subject: 

I'm pretty sure this:

code:

let qux (wooble : foo) =
     print_int wooble#baz;
     print_newline ();;


becomes this:
code:

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.

Author:  wtd [ Wed Dec 20, 2006 8:16 pm ]
Post subject: 

Not quite. Smile

Author:  richcash [ Fri May 25, 2007 6:20 pm ]
Post subject:  Re: [TYS] O'Caml TYS for the day

I know this is old, but I'm bored. Wink


No change is necessary for objects of class bar to be passed to qux.

Ocaml:
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 :
code:
foo -> unit

But the compiler simplifies this by replacing the type of foo in automatically :
code:
(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 :
Ocaml:
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.

Author:  wtd [ Sat May 26, 2007 12:09 am ]
Post subject:  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?

Author:  richcash [ Sat May 26, 2007 4:15 pm ]
Post subject:  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 :

Ocaml:
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
code:
< baz : int; .. >
which means allow any object that has a method (baz : int), regardless of what other methods the object might have. Smile

Author:  wtd [ Sun May 27, 2007 1:29 am ]
Post subject:  RE:[TYS] O\'Caml TYS for the day

Alternatively:

code:
let qux wooble = Printf.printf "%d\n" wooble#baz

Author:  richcash [ Sun May 27, 2007 2:07 am ]
Post subject:  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. Razz

Author:  wtd [ Sun May 27, 2007 2:41 am ]
Post subject:  RE:[TYS] O\'Caml TYS for the day

I made it better! Smile


: