Julia: Use of variable name for keyword argument key

Created on 19 Jun 2018  Â·  3Comments  Â·  Source: JuliaLang/julia

Currently, keyword arguments must explicitly state the key and value being referenced in a call. However, this can be fairly verbose when both the value variable and kwarg key share the same name (e.g. f(; value = value)).

This issue proposes that f(; value) be interpreted as f(; value = value). A precedent for this style comes from ES6, namely { value } as the shorthand for { value: value }.

color = :green
printstyled("test"; color = color)  # current
printstyled("test", color)          # proposed

This is especially helpful for functions which recursively propagate keyword arguments:

f(x; kwarg) = x ≤ 0 ? kwarg : ("(" * f(x-1; kwarg = kwarg) * " | " * f(x-2; kwarg = kwarg) * ")")
f(x; kwarg) = x ≤ 0 ? kwarg : ("(" * f(x-1; kwarg) * " | " * f(x-2; kwarg) * ")")

The existing convention for right-precedence would not be affected.

keyword arguments

Most helpful comment

I believe @davidanthoff suggested this during the discussion on named tuples, and indeed it's already implemented in julia-syntax.scm and would just need to be un-commented :)

All 3 comments

I believe @davidanthoff suggested this during the discussion on named tuples, and indeed it's already implemented in julia-syntax.scm and would just need to be un-commented :)

If this were done it should be conditional on using semicolon to denote the start of keywords

printstyled("test"; color)

Otherwise this convenience could end up calling unintended methods. Or at least it would be quite ambiguous

Closing since most of the discussion is on the dup.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yurivish picture yurivish  Â·  3Comments

manor picture manor  Â·  3Comments

StefanKarpinski picture StefanKarpinski  Â·  3Comments

TotalVerb picture TotalVerb  Â·  3Comments

wilburtownsend picture wilburtownsend  Â·  3Comments