* [Caml-list] Types of classes (was: Re: "ocaml_beginners"::[] OCaml tutorial) [not found] ` <87adcj8umf.dlv@wanadoo.fr> @ 2003-06-16 12:42 ` Richard Jones 2003-06-16 14:04 ` [Caml-list] Types of classes Remi Vanicat 0 siblings, 1 reply; 2+ messages in thread From: Richard Jones @ 2003-06-16 12:42 UTC (permalink / raw) To: caml-list On Sun, Jun 15, 2003 at 01:09:44PM +0200, Remi Vanicat wrote: > Well, I've spot[ted] another mistake : > > http://merjis.com/richj/computers/ocaml/tutorial/ch8.shtml > > Say (about function acting over object) : > > " > We can force OCaml to be more specific and only allow drain_stack to > be called on 'a stacks by narrowing the type of the s argument, like > this: > > # let drain_stack (s : 'a stack) = > while s#size > 0 do > ignore (s#pop) > done;; > val drain_stack : 'a stack -> unit = <fun> > " > > this is not true, the type 'a stack is only an alias for the type : > > < push : 'a -> unit; pop : 'a; peek : 'a; size : int > > > and, as such, drain_stack can be called (for example) with an > argument of class stack_of_ints, but also on a object of the following > class : > > class ['a] foo (x : 'a) = > object > method push (y : 'a) = () > method pop = x > method peek = x > method size = 1 > end Isn't this a bug in OCaml? Is there a way to force a specific type for an arg to a function? Rich. -- Richard Jones, Red Hat Inc. (London) and Merjis Ltd. http://www.merjis.com/ http://www.annexia.org/ Freshmeat projects: http://freshmeat.net/users/rwmj 'There is a joke about American engineers and French engineers. The American team brings a prototype to the French team. The French team's response is: "Well, it works fine in practice; but how will it hold up in theory?"' ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Types of classes 2003-06-16 12:42 ` [Caml-list] Types of classes (was: Re: "ocaml_beginners"::[] OCaml tutorial) Richard Jones @ 2003-06-16 14:04 ` Remi Vanicat 0 siblings, 0 replies; 2+ messages in thread From: Remi Vanicat @ 2003-06-16 14:04 UTC (permalink / raw) To: caml-list Richard Jones <rich@annexia.org> writes: > On Sun, Jun 15, 2003 at 01:09:44PM +0200, Remi Vanicat wrote: >> this is not true, the type 'a stack is only an alias for the type : >> >> < push : 'a -> unit; pop : 'a; peek : 'a; size : int > >> >> and, as such, drain_stack can be called (for example) with an >> argument of class stack_of_ints, but also on a object of the following >> class : >> >> class ['a] foo (x : 'a) = >> object >> method push (y : 'a) = () >> method pop = x >> method peek = x >> method size = 1 >> end > > > Isn't this a bug in OCaml? Is there a way to force a specific type > for an arg to a function? But < push : 'a -> unit; pop : 'a; peek : 'a; size : int > is a specific type. stack is not exactly a type it is a class, and as such, when seen as a type, it is just an alias of the former. The type of an object is not the name of its class, it is the list of its method with their type. Then, for conciseness and readability, caml use the name of class as an alias for the corresponding type, but this is nothing else, an alias, not a new type. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2003-06-16 14:02 UTC | newest] Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <20030614172512.GA1255@redhat.com> [not found] ` <878ys4zeis.dlv@wanadoo.fr> [not found] ` <20030614200747.GA2300@redhat.com> [not found] ` <87adcj8umf.dlv@wanadoo.fr> 2003-06-16 12:42 ` [Caml-list] Types of classes (was: Re: "ocaml_beginners"::[] OCaml tutorial) Richard Jones 2003-06-16 14:04 ` [Caml-list] Types of classes Remi Vanicat
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox