caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Erkki Seppala <flux-caml@inside.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] destructive local opens
Date: Thu, 20 Aug 2015 22:19:12 +0300	[thread overview]
Message-ID: <m491texepjj.fsf@coffee.modeemi.fi> (raw)
In-Reply-To: <CAN6ygOnP7rb91e+8=v+As-nWxDLaOJ5vwAuPE+exnhG4F+CpDw@mail.gmail.com> (Yotam Barnoy's message of "Thu, 20 Aug 2015 10:03:50 -0700")

Hi,

Couple points I felt the need to address :).

Yotam Barnoy <yotambarnoy@gmail.com> writes:

> A lot of great ideas here. I want to comment on many of them, so
> please bear with me while I summarize some of the points in the
> process. Hopefully this will also help anybody new to the discussion,
> which has become extremely long.
>
> - Local opens are somewhat of an anti-pattern in OCaml, because they're
> usually used in places where you have the same names defined in
> multiple modules:

While this is certainly a very popular use case in particular when
dealing with operators, I find myself using the local 'let open' form
very often in other contexts. In fact, I don't ever seem to find myself
using it for redefining identifiers. Examples I grepped from my sources,
admittedly some obsoleted by the new disambiguation feature:

2014/device-table/layout.ml:
  let open Containers_misc.PrintBox in
  hlist ~bars:false [box; text (String.make n ' ')]

2014/TinyGUploader/src/upload.ml:
  let open Gcode.Parser in
  match input with
  | Move { move_reg = (G0 | G1); move_pos = at } -> ..

2014/TinyGUploader/src/upload.ml:
  let open ANSITerminal in
  let progress = float n /. float input_nlines in
  move_bol ();
  let time_left = (Unix.gettimeofday () -. t0) /. progress in
  let time_finished = t0 +. time_left in
  if not common_options.co_verbose then (
    printf [Bold; green; on_default] "%2.1f%%" (100.0 *. progress);
    printf [default] "complete, %d/%d, ETA " n input_nlines;
    printf [Bold; green; on_default] "%s (%s)"
           (Utils.human_eta (int_of_float time_left))
           (Utils.string_of_time time_finished);
    erase Eol;
  )

2014/TinyGUploader/src/align.ml:
let render_gcode cairo mapping_matrix gcode =
  let open Cairo in
  set_line_width cairo (length_in_matrix mapping_matrix 3.0);
  set_line_cap cairo ROUND;
  set_line_join cairo JOIN_ROUND;
  ..

2014/TinyGUploader/src/align.ml:
  let open Bigarray in
  let open Array1 in
  let ba = create int8_unsigned c_layout (String.length str) in
  for c = 0 to String.length str - 1 do
    ba.{c} <- Char.code str.[c]
  done;
  ba

Many of these would be more verbose without using a local open, the only
non-verbose alternate being opening the modules for the whole remaining
module. Using the local open gives the person reading the code a heavy
hint that this code will be dealing a lot with the concepts of that
module.

> The problem is, if you ever change one of the modules to include
> another function that wasn't expected originally (for example, N now
> includes *), you now have subtle bugs breaking your code in completely
> separate files from the ones you were editing, and the type system
> can't necessarily do anything to catch these errors.

I think this is an instance where theory and practice don't match: in
practice, I don't ever remember having witnessed such a bug.

> - Similar languages to OCaml (such as Haskell) outlaw having the same
> name defined twice in the same scope. OCaml only issues a warning.
> IMO, this warning should be turned into an error.

I don't think these is such a warning in general. I referred to
http://caml.inria.fr/pub/docs/manual-ocaml/comp.html . Do you mean only
definitions, not expression scopes?

I in fact find it quite convenient to be able to write:

let furbulation = acquire_value () in
let furbulation, brightness = process_step_1 furbulation in 
let furbulation, volume = process_step_2 (brightness / 2) furbulation in
..

The alternative of renaming intermediate steps can result in mistakes if
the order of steps is to be changed, as can happen when doing signal
processing algorithms in an explorative fashion.

Cheers,
-- 
  _____________________________________________________________________
     / __// /__ ____  __               http://www.modeemi.fi/~flux/\   \
    / /_ / // // /\ \/ /                                            \  /
   /_/  /_/ \___/ /_/\_\@modeemi.fi                                  \/

  reply	other threads:[~2015-08-20 19:19 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-03 13:39 Nils Becker
2015-08-03 13:43 ` Thomas Refis
2015-08-03 13:45 ` Daniel Bünzli
2015-08-03 13:47   ` Daniel Bünzli
     [not found]     ` <55BF75F6.1040006@bioquant.uni-heidelberg.de>
2015-08-03 14:24       ` Daniel Bünzli
2015-08-03 14:37         ` Gabriel Scherer
2015-08-03 14:43           ` Daniel Bünzli
2015-08-03 15:10           ` octachron
2015-08-03 15:22             ` Daniel Bünzli
2015-08-03 16:13               ` octachron
2015-08-03 16:51                 ` Daniel Bünzli
2015-08-03 17:18                   ` Hendrik Boom
2015-08-03 17:59                   ` octachron
2015-08-06 13:23                     ` RE : " moreno pedro
2015-08-04  6:51         ` Petter Urkedal
2015-08-04  9:33           ` Goswin von Brederlow
2015-08-05  6:40             ` Petter A. Urkedal
2015-08-05 10:16               ` David Allsopp
2015-08-06  9:35               ` Goswin von Brederlow
2015-08-04 13:50           ` Hendrik Boom
2015-08-04  9:26         ` Goswin von Brederlow
2015-08-04  9:38           ` Daniel Bünzli
2015-08-04 12:26             ` vrotaru.md
2015-08-04 13:12               ` David Allsopp
2015-08-04 13:17                 ` Jeremy Yallop
2015-08-04 13:54                   ` vrotaru.md
2015-08-04 15:25                   ` Drup
2015-08-04 22:22                     ` vrotaru.md
2015-08-04 22:55                       ` Hendrik Boom
2015-08-05  4:52                         ` Gabriel Scherer
2015-08-04 13:14               ` Ivan Gotovchits
2015-08-14 10:55                 ` Goswin von Brederlow
2015-08-14 11:28                   ` Drup
2015-08-18 11:11                     ` Goswin von Brederlow
2015-08-18 12:52                       ` David Allsopp
2015-08-18 13:00                         ` Gabriel Scherer
2015-08-18 22:26                           ` Anthony Tavener
2015-08-19 13:55                             ` Oleg
2015-08-19 14:13                               ` John Whitington
2015-08-19 15:47                                 ` Hendrik Boom
2015-08-19 15:52                             ` Hendrik Boom
2015-08-19 18:09                               ` Anthony Tavener
2015-08-19 15:55                             ` Simon Cruanes
2015-08-19 16:42                               ` Arthur Wendling
2015-08-19 21:15                               ` octachron
2015-08-20  8:06                                 ` Romain Bardou
2015-08-20 17:03                                   ` Yotam Barnoy
2015-08-20 19:19                                     ` Erkki Seppala [this message]
2015-08-06  9:23               ` Goswin von Brederlow
2015-08-06  9:21             ` Goswin von Brederlow
2015-08-06 10:19               ` Daniel Bünzli
2015-08-06 13:36                 ` Hendrik Boom
2015-08-14 10:57                   ` Goswin von Brederlow
2015-08-17 10:17 Nils Becker
2015-08-17 14:26 ` Gabriel Scherer
2015-08-17 15:11   ` Nils Becker
2015-08-17 15:17     ` Drup
2015-08-17 15:18     ` Gabriel Scherer
2015-08-17 18:31       ` Hendrik Boom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m491texepjj.fsf@coffee.modeemi.fi \
    --to=flux-caml@inside.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).