One more to go:
f(a=1, (b=2,), c=3)
but there are actually four syntax surprises contained in that example:
- It’s possible to have superfluous parens around keyword args.
- It’s possible to have superfluous parens around
NamedTuple
fieldname-value pairs. - Keyword args are allowed in non-final position,
- 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:
- it presents text that very much resembles a call to its constructor, but it’s not, and
- in this context it promotes the idea that
Pair
s ofkey => value
is a preferred way to package and send keyword arguments around, when infact that causes type-instability (see for example, here).