caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Basic quation about object methods
@ 2004-03-23 11:57 Jere Sanisalo
  2004-03-23 13:35 ` Jacques Garrigue
  0 siblings, 1 reply; 2+ messages in thread
From: Jere Sanisalo @ 2004-03-23 11:57 UTC (permalink / raw)
  To: caml-list

Hi,

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

Still both of these methods work just the same. 

Thanks for any info.

-- 
Jere Sanisalo [xm@xmunkki.org] - http://www.xmunkki.org/

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


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

* Re: [Caml-list] Basic quation about object methods
  2004-03-23 11:57 [Caml-list] Basic quation about object methods Jere Sanisalo
@ 2004-03-23 13:35 ` Jacques Garrigue
  0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2004-03-23 13:35 UTC (permalink / raw)
  To: xm; +Cc: caml-list

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


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

end of thread, other threads:[~2004-03-23 13:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-23 11:57 [Caml-list] Basic quation about object methods Jere Sanisalo
2004-03-23 13:35 ` Jacques Garrigue

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