caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Type notation in OO-layer
@ 2007-08-03 21:18 Oliver Bandel
  2007-08-03 21:29 ` [Caml-list] " Oliver Bandel
  2007-08-04  3:28 ` Julien Moutinho
  0 siblings, 2 replies; 4+ messages in thread
From: Oliver Bandel @ 2007-08-03 21:18 UTC (permalink / raw)
  To: caml-list

Hello,

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 verysimple.ml
class simple_1 =
  object
    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
  end

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 verysimple.ml
iprint: 12
value_as_string: 12
vas:             12
iprint: 77
value_as_string: 77
vas:             77
oliver@siouxsie2:~/ocaml-oo$ ocamlc -i  verysimple.ml
class simple_1 :
  object
    val mutable mutval : int
    method get : int
    method set : int -> unit
    method value_as_string : string
    method vas : unit -> string
  end
val iprint : int -> unit
val example_s1 : simple_1
oliver@siouxsie2:~/ocaml-oo$


=======================================================


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?

TIA,
   Oliver


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-04  3:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-03 21:18 Type notation in OO-layer Oliver Bandel
2007-08-03 21:29 ` [Caml-list] " Oliver Bandel
2007-08-04  0:38   ` Jacques GARRIGUE
2007-08-04  3:28 ` Julien Moutinho

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).