caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Naming conventions
@ 2002-05-08 13:10 Paul Stodghill
  2002-05-08 14:04 ` Ken Wakita
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Stodghill @ 2002-05-08 13:10 UTC (permalink / raw)
  To: Caml Mailing List (E-mail)

In Scheme, there is a conventtion that the names of destructive functions end with "!" and predicates end with "?". E.g., "append!" vs. "append", and "null?", "pair?", etc.

Are there any similar conventions that people use in O'Caml?

I ask because I am implementing a class and I want to provide both destructive and non-destructive versions of some of the methods. I would like the method names to clearly indicate which is which.

Thanks.
-------------------
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] 6+ messages in thread
* RE: [Caml-list] Naming conventions
@ 2002-05-08 13:31 Gregory Morrisett
  0 siblings, 0 replies; 6+ messages in thread
From: Gregory Morrisett @ 2002-05-08 13:31 UTC (permalink / raw)
  To: Paul Stodghill, Caml Mailing List (E-mail)

> In Scheme, there is a conventtion that the names of 
> destructive functions end with "!" and predicates end with 
> "?". E.g., "append!" vs. "append", and "null?", "pair?", etc.
> 
> Are there any similar conventions that people use in O'Caml?

I don't think there's a convention.  Part of the reason is that it
tends to be manifest in the type whether or not an operation is
destructive and whether or not it is a predicate.  Consider:

  val push : 'a -> 'a stack -> 'a stack  (* functional *)

versus

  val push : 'a -> 'a stack -> unit        (* imperative *)

That's not to say that it wouldn't be good to have a convention,
especially when something has an interface that looks functional
but does some observable side effect.  Furthermore, it's often
hard to tell constructors from predicates when reading code
(i.e., does "empty" construct an empty stack or is it a predicate
that returns true on an empty stack?) 

I tend to use imp_<id> (as in imp_push) to reflect the fact that 
something imperative is going on, and is_<id> for predicates
(as in is_empty).  

-Greg
-------------------
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] 6+ messages in thread
* Re: [Caml-list] Naming conventions
@ 2002-05-08 13:33 Krishnaswami, Neel
  2002-05-08 13:59 ` Dave Mason
  0 siblings, 1 reply; 6+ messages in thread
From: Krishnaswami, Neel @ 2002-05-08 13:33 UTC (permalink / raw)
  To: Caml Mailing List (E-mail)

Paul Stodghill [mailto:stodghil@CS.Cornell.EDU] wrote:
>
> In Scheme, there is a conventtion that the names of
> destructive functions end with "!" and predicates end with
> "?". E.g., "append!" vs. "append", and "null?", "pair?", etc.
>
> Are there any similar conventions that people use in O'Caml?

Yes. The usual convention is that mutating functions return the
unit value. Eg:

  Hashtbl.add : 'a t -> key -> data:'a -> unit (* Mutable collection   *)
  Map.add     : key -> 'a -> 'a t -> 'a t      (* Immutable collection *)

Each type usually has two iterators, as well -- one named "fold" for
the usual applicative folds, and another named "iter" for folding 
a destructive procedure over a datastructure. Eg:

  fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
  iter : (key -> 'a -> unit) -> 'a t -> unit

Since the imperative and applicative versions have different type, I 
suppose this convention might cause problems if you want to be 
able to transparently replace the pure versions with the destructive
versions. In that case, I'd suggest inventing your own convention;
perhaps you could use unprimed and primed names with the same types,
to denote pure and destructive operations. Eg;

  Mytype.add  : key -> 'a -> 'a t -> 'a t (* Pure *)
  Mytype.add' : key -> 'a -> 'a t -> 'a t (* Imperative *)

--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
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] 6+ messages in thread

end of thread, other threads:[~2002-05-08 17:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-08 13:10 [Caml-list] Naming conventions Paul Stodghill
2002-05-08 14:04 ` Ken Wakita
2002-05-08 13:31 Gregory Morrisett
2002-05-08 13:33 Krishnaswami, Neel
2002-05-08 13:59 ` Dave Mason
2002-05-08 17:12   ` Pierre Weis

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