caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Type annotations [Was: Are record types generative?]
@ 2018-01-26 11:03 Oleg
  2018-01-26 14:41 ` Cedric Cellier
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg @ 2018-01-26 11:03 UTC (permalink / raw)
  To: caml-list


# Perhaps the subthread on type annotations should get its own
# subject...

> On Tue, Jan 23, 2018 at 6:14 PM, Hendrik Boom wrote:
>
>     I'm starting to think that the ability to write OCaml programs without
>     mentioning the types is a drawback in the language, because it makes
>     programs hard to understand.

I strongly recommend students to type-annotate all functions they
write. In fact, they should start writing code by writing out its
type. If they don't have a good idea what interface the function
should have, it's very likely that they haven't really thought the
problem through. In that case, they should think more about what it is
they want to do, rather than jumping into writing code they have only
a vaguest idea of.

I follow the same advice myself. For example, suppose I want to write 
an accumulating map, which maps elements of a list and accumulates
something along the way (this function proved very handy, at least in
MetaOCaml implementation). The first this is to decide on the
interface:

let rec map_accum : 
  ('accum -> 'a -> 'b * 'accum) -> 'accum -> 'a list -> 'b list * 'accum = 

after which the code is easy to write

 fun f acc -> function
    | []   -> ([],acc)
    | h::t -> 
        let (h,acc) = f acc h in
        let (t,acc) = map_accum f acc t in
        (h::t, acc)

One clear benefit of the explicit annotation is that the type errors
become much more meaningful and precise (which is especially important
for students).

Granted, sometimes the type of some complicated callback could be
too complicated to spell out (which is often the indication that one
should give it a name). In that case, the underscore is handy:

let rec map_accum : 
  _ -> 'accum -> 'a list -> 'b list * 'accum = ...

In short, if we know that a function is to take three arguments and
return an int, why not to write it down. It also helps reading the
code later on (without clicking on various identifiers: I can read much
faster than I can click).

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

* Re: [Caml-list] Type annotations [Was: Are record types generative?]
  2018-01-26 11:03 [Caml-list] Type annotations [Was: Are record types generative?] Oleg
@ 2018-01-26 14:41 ` Cedric Cellier
  0 siblings, 0 replies; 2+ messages in thread
From: Cedric Cellier @ 2018-01-26 14:41 UTC (permalink / raw)
  To: caml-list

-[ Fri, Jan 26, 2018 at 08:03:25PM +0900, Oleg ]----
> In short, if we know that a function is to take three arguments and
> return an int, why not to write it down.

Because sometime the compiler does a better job at guessing more generic types
than what I had in mind, which might came handy later when/if I do need that
more generic function?
Or merely because I actualy enjoy giving only the vaguest indications to the
compiler and have it walk me toward the actual thing that I wanted to say :)


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

end of thread, other threads:[~2018-01-26 14:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-26 11:03 [Caml-list] Type annotations [Was: Are record types generative?] Oleg
2018-01-26 14:41 ` Cedric Cellier

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