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.
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:
becomes this:
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. |
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. No change is necessary for objects of class bar to be passed to qux.
The above code compiles and runs as expected. The type for the qux function is :
But the compiler simplifies this by replacing the type of foo in automatically :
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 :
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 :
We give wooble the special type signature
|
Author: | wtd [ Sun May 27, 2007 1:29 am ] | ||
Post subject: | RE:[TYS] O\'Caml TYS for the day | ||
Alternatively:
|
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. |
Author: | wtd [ Sun May 27, 2007 2:41 am ] |
Post subject: | RE:[TYS] O\'Caml TYS for the day |
I made it better! |