Version française
Home     About     Download     Resources     Contact us    
Browse thread
RE: Real excellent object oriented source code examples of Ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Mattias Waldau <mattias.waldau@a...>
Subject: RE: Real excellent object oriented source code examples of Ocaml
The reason I don't use the OO-features is not because I don't like OO, but
since Ocaml support for OO isn't as good as it could be.

The OO-features are just built on top of the normal environment. For
example, the toplevel don't understand objects, you just get the message "-
: with_oo = <obj>". The support for terms is much better. Thus, my
Caml-programs have the structure of OO, but I code them without the
OO-features of Ocaml. For example, I wouldn't normally write the
'with_oo'-class below, instead I code it as 'nooo' below.

However, if the support for showing/browsing objects on the toplevel would
be better (or maybe it is already there and I should just read the manual
better), I will start using OO in Ocaml.

The only problem with OO is that OO and functional programs don't mix well,
since OO is built on state-change. My background is from functionalal and
logic programming.

/mattias

P.s. How is the speed of methods calls, object creation compare to standard
calls and creating terms?

WITH OO:
========

class with_oo =
object
  val mutable x = 0
  val mutable y = 0
  method get_x = x
  method move d = x <- x + d; y <- y + d
end

(*

# let t = new with_oo;;
val t : with_oo = <obj>
# t;;
- : with_oo = <obj>
# t#get_x;;
- : int = 0
# t#move 10;;
- : unit = ()
# t#get_x;;
- : int = 10
#

*)

SAME CODE WITHOUT OO:
=====================

type nooo = {
  mutable x:int;
  mutable y:int;
  }

let nooo_init =
  { x=0; y=0 }

let nooo_get_x this = this.x

let nooo_move this d =
  this.x <- this.x + d;
  this.y <- this.y + d


(*

# let t = nooo_init;;
val t : nooo = {x=0; y=0}
# nooo_get_x t;;
- : int = 0
# nooo_move t 10;;
- : unit = ()
# t;;
- : nooo = {x=10; y=10}

*)