Syntax Surprises

One more to go:

f(a=1, (b=2,), c=3)

but there are actually four syntax surprises contained in that example:

  1. It’s possible to have superfluous parens around keyword args.
  2. It’s possible to have superfluous parens around NamedTuple fieldname-value pairs.
  3. Keyword args are allowed in non-final position,
  4. even if they are succeeded by a semicolon!

Would you expect this?

julia> f((a=1, b=2), (c=3); d=4)
(args = ((a = 1, b = 2),), kwargs = Base.Pairs(:c => 3, :d => 4))

That’s not at issue here. At issue is a break from Julia’s convention that, where reasonable, an object’s show method should print working code which illustrates how to construct the object.

Obviously Julia doesn’t print working code for vectors, matrices, and functions, but it’s often unreasonable to try, and at least for those it’s pretty obvious that what has been printed isn’t executable code.

The show method for Base.Pairs here has two WATs:

  1. it presents text that very much resembles a call to its constructor, but it’s not, and
  2. in this context it promotes the idea that Pairs of key => value is a preferred way to package and send keyword arguments around, when infact that causes type-instability (see for example, here).
2 Likes