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:10 [Caml-list] Naming conventions Paul Stodghill
@ 2002-05-08 14:04 ` Ken Wakita
  0 siblings, 0 replies; 6+ messages in thread
From: Ken Wakita @ 2002-05-08 14:04 UTC (permalink / raw)
  To: Paul Stodghill, caml-list


This is a feature I would like to have.  In Scheme, I used to put '*' at the
end of a list of elements and '+' at the end of a list of one or more
elements.

Ken Wakita

> From: "Paul Stodghill" <stodghil@CS.Cornell.EDU>
> Date: Wed, 8 May 2002 09:10:53 -0400
> To: "Caml Mailing List (E-mail)" <caml-list@inria.fr>
> Subject: [Caml-list] Naming conventions
> 
> 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

-------------------
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:59 ` Dave Mason
@ 2002-05-08 17:12   ` Pierre Weis
  0 siblings, 0 replies; 6+ messages in thread
From: Pierre Weis @ 2002-05-08 17:12 UTC (permalink / raw)
  To: Dave Mason; +Cc: caml-list

[...]
> But, the original question was about objects.  In Java there is a
> style that is fairly convenient, whereby mutators for objects return
> the object itself instead of void (unit).  This allows doing:
[...]

Oh, please, no!

We had once these style of primitives in Caml and found it really
error prone: you just confuse procedures and functions!

A procedure is called for its side effects, hence it does not return
any meaningful value, and hence it reflects this fact in its return
type that should be unit.

Thanks to this convention, there is a special warning to help the
programmer not to forget an argument to a procedure call inside a
sequence of side-effects: if f has type int -> int -> unit and you
compile the sequence

begin 
 f 1;
 print_newline ()
end

then you will have a warning for the incomplete call f 1 (telling you
that this expression should have type unit).

If your procedures wrongly return a value which is not unit, then you
will have warnings everywhere; hence, you will disable warnings;
hence, you will miss partially applied procedure calls.

When programming in the large, such silently ignored side-effects are
a nightmare to find out, particularly if the given procedure has a lot
of arguments (imagine forgetting just the last argument to mult_nat, that
is just providing eight out of nine of the required arguments! It is
easy to find if the compiler points it out for you, otherwise you can
read the program over and over again without noticing the missing
arg).

So no. This ``fairly convenient style'' is not your friend; it is in
fact as convenient as programming without a type-checker: seemingly
``cool'' at the beginning, but really harder in the long run.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
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
  2002-05-08 17:12   ` Pierre Weis
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Mason @ 2002-05-08 13:59 UTC (permalink / raw)
  To: Caml Mailing List (E-mail)

For non-OO style Ocaml, I agree that the type signature is usually
enough, although I also often use is_foo or fooP as predicates.

But, the original question was about objects.  In Java there is a
style that is fairly convenient, whereby mutators for objects return
the object itself instead of void (unit).  This allows doing:

    let theLeftFoo = new foo(42) in
        theLeftFoo.setX(...)
                  .setY(...)
                  .initBar()

as opposed to:

    let theLeftFoo = new foo(42) in
	theLeftFoo.setX(...);
        theLeftFoo.setY(...);
        theLeftFoo.initBar();
        theLeftFoo

I am undecided whether I think this is a good style or not, but it is
more readable if you like to give meaningful (and hence long) variable
names.  (If I'd used x instead of theLeftFoo, you probably wouldn't
have noticed much difference.)

../Dave
-------------------
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

* 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

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