The ergonomics of labels

A few quick thoughts on labels:

  • We like labels, and liked them well before we had tons of people to onboard.
  • Consistency helps everyone. This old post is still worth reading I think.
  • We like ~f because for many iterators, it’s sometimes easier to read with the function first (and with the function partially applied), and in an almost CPS-like style, where the (large) function body goes second.
  • Labels are great for disambiguating arguments of the same type. Look at String.sub:
    var sub : ?pos:int -> ?len:int -> string -> string 
    
  • They also serve as documentation for the meaning of a function, where the type alone isn’t enough, even if there isn’t another value to confuse it with. e.g., look at String.Escaping.index, whose signature is:
     val index : string -> escape_char:char -> int option`.
    
    the escape_char clarifies the purpose of that argument.
  • The combination of labeled arguments, label punning, and record field punning encourages you to carry the same names across your codebase, which again can improve clarity and uniformity.
11 Likes