caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@inria.fr
Subject: Type notation in OO-layer
Date: Fri,  3 Aug 2007 23:18:41 +0200	[thread overview]
Message-ID: <1186175921.46b39bb1508b1@webmail.in-berlin.de> (raw)

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


             reply	other threads:[~2007-08-03 21:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-03 21:18 Oliver Bandel [this message]
2007-08-03 21:29 ` [Caml-list] " Oliver Bandel
2007-08-04  0:38   ` Jacques GARRIGUE
2007-08-04  3:28 ` Julien Moutinho

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1186175921.46b39bb1508b1@webmail.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).