caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: xm@xmunkki.org
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Basic quation about object methods
Date: Tue, 23 Mar 2004 22:35:51 +0900	[thread overview]
Message-ID: <20040323223551D.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20040323135747.A3439@xmunkki.org>

From: Jere Sanisalo <xm@xmunkki.org>

> I'm a relatively newbie ocaml user, and I'm currently trying to understand a
> basic thing about methods. Can someone clarify the following:
> 
> class test =
>   object
>     val mutable v = 0
>     method no_params = v<-v+1; print_int v
>     method has_params () = v<-v+1; print_int v
>   end;;
> 
> Now what's the basic difference between methods "no_params" and
> "has_params"? The o'reilly book says "Note that some methods do not need
> parameters" and "We usually access instance variables with parameterless
> methods.".

There is no difference (except that with has_params you will have to pass
unit to call the method). The reason is that internally a method takes
the object itself as first argument, so no_params is actually a
1-argument function, while has_params is a 2-argument function.

But for practical reasons, it may be nice to add a parameter to
methods causing side-effects. This makes easier to distinguish them
from effect-free ones, and also lets you extract the method itself by
calling it without parameter, when you want to pass it around.
All this is purely practical, since there is really no theoretical
difference between the two.

Jacques Garrigue 

-------------------
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


      reply	other threads:[~2004-03-23 13:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-23 11:57 Jere Sanisalo
2004-03-23 13:35 ` Jacques Garrigue [this message]

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=20040323223551D.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=xm@xmunkki.org \
    /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).