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
Type notation in OO-layer
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oliver Bandel <oliver@f...>
Subject: Type notation in OO-layer

please look at this very simple OO-stuff
to discuss a question I have, regarding the
notation (and / or behaviour) of the OO-layer
in OCaml:

oliver@siouxsie2:~/ocaml-oo$ cat
class simple_1 =
    val mutable mutval = 12

    method get = mutval

    method set x = mutval <- x

    method value_as_string = Printf.sprintf "value_as_string: %d" mutval
    method vas ()          = Printf.sprintf "vas:             %d" mutval

let iprint i = Printf.printf "iprint: %d\n" i

let example_s1 = new simple_1

let _ =
  let o_1 = new simple_1 in

  iprint (o_1 # get);
  print_endline (o_1 # value_as_string);
  print_endline (o_1 # vas());
  o_1#set 77;
  iprint (o_1 # get);
  print_endline (o_1 # value_as_string);
  print_endline (o_1 # vas());
oliver@siouxsie2:~/ocaml-oo$ ocaml
iprint: 12
value_as_string: 12
vas:             12
iprint: 77
value_as_string: 77
vas:             77
oliver@siouxsie2:~/ocaml-oo$ ocamlc -i
class simple_1 :
    val mutable mutval : int
    method get : int
    method set : int -> unit
    method value_as_string : string
    method vas : unit -> string
val iprint : int -> unit
val example_s1 : simple_1


As you can see, the methods "value_as_string"
and "vas" are intended to do the same: giving back
a string, that will be created from the internal int-value.

Following the non-OO programming in OCaml,
vas() should be the right way to do it, because
the method get's no arg.
Following the OO-like way, value_as_string should be OK also.

What would be the right way?

value_as_string has type "string", but that is not completely correct, because
it get's no input-value, and therefore is of type "unit -> string".

One could say, that this is a special notation for OO, but
if we are rigid (we should be! ... shouldn't we?!) it is not correct.

As it is not a true "unit"-function, we at least should give it a
unit-like type like "message -> string" so that the type-system
make a complete annotation of type?!

Why is the "sending a message to the method" activity not
notated in the type?

And: are both definitions correctly?
Which to choose? Preferences in style?