Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Why can't I call a function over a subclass?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-05 (11:08)
From: Vincent Aravantinos <vincent.aravantinos@y...>
Subject: Re: [Caml-list] Why can't I call a function over a subclass?

Le 5 oct. 07 à 10:08, Luca de Alfaro a écrit :

> I don't understand this.  I thought the whole idea of oo  
> programming was that you could use a subclass in place of a  
> superclass...
> I also thought that the strict typing of Ocaml was there to avoid  
> runtime errors, but if so, why does it complain in this situation,  
> when no runtime error can possibly happen?
> It seems to me that the idea of oo programming is broken for no  
> good reason...

- This is absurd in an oo context. Here, the main paradigm is not oo.  
It is simply strongly typed. So if you write that a function expects  
a particular type, this function should be applied with this  
particular type and nothing else. *You* wrote this so you claimed for  

You want more generality ? Then let the type checker do its job as  
Pietro said. *The default behaviour is not to write any type info.*  
If you stay with this you get the standard oo behaviour.

Annotations are there to give more info to the compiler in case it  
does infer what you have in mind.

- Strict typing of Ocaml is not to avoid runtime errors. It simply  
stays that if two types expected to be the same differ, we suppose  
*the program does not match what the programmer has in mind*. As a  
little side-effect, this can avoid runtime errors :)

If you keep this idea of "what the programmer has in mind", in this  
particular case you said to the compiler with your type annotation  
that what you had in mind was that the function only takes a r. What  
you said to the compiler was not what you had in mind.

And I insist again: the default behaviour (don't anotate) gives you  
what you would expect.