caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Syntax puzzle / suggestion
@ 2019-08-09 14:27 Richard W.M. Jones
  2019-08-09 14:40 ` Gabriel Scherer
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Richard W.M. Jones @ 2019-08-09 14:27 UTC (permalink / raw)
  To: caml-list

Let's imagine you have a list of named things, but you also want to
collect them up into a single list.  For example:

  let item1 = "hello"
  let item2 = "world"
  let all_items = [ item1; item2 ]

  let () =
    printf "item1 = %s\n" item1;
    printf "all_items = %s\n" (String.concat ", " all_items)

This is fine, but there's a danger that a programmer could add item3
but forget to add it to the "all_items" list.  (In the real world
problem to which this applies, my items are complex and lengthy
structures, and the "all-things-list" is well off the bottom of the
page when you're viewing the top item).

My idea to fix this was to write:

  let all_items = [
    ("hello" as item1);
    ("world" as item2);
  ]

Actually I was slightly surprised to find this doesn't compile.  I
guess because "as" can only be used like this in patterns, not
expressions.  Also the scoping is wrong because the "as item1" if it
worked probably wouldn't apply outside the list.

I haven't worked out if it's possible to write this in ordinary OCaml,
although I suppose ppx could solve it.  Any ideas if this is possible?

Rich.

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

end of thread, other threads:[~2019-08-10 20:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09 14:27 [Caml-list] Syntax puzzle / suggestion Richard W.M. Jones
2019-08-09 14:40 ` Gabriel Scherer
2019-08-09 14:45   ` Richard W.M. Jones
2019-08-09 14:48 ` Martin Riener
2019-08-10  5:19   ` rixed
2019-08-09 14:51 ` Josh Berdine
2019-08-10 15:18   ` Oliver Bandel
2019-08-10 20:34   ` Chet Murthy
2019-08-09 14:56 ` Frédéric Bour

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