NamedArgs

Documentation for NamedArgs.

This package allows naming arguments in function calls.

Usage

Adding the @na macro in front of a function call allows to use the syntax arg=value also for non-keyword arguments:

@na my_function(name="Federico", age=35; happy=true, location=missing)

The function call must contain a semicolon, arguments must be on the left, keyword arguments on the right.

Examples

julia> using NamedArgs
julia> foo(n::Integer, r::Real; offset=100) = n + r + offsetfoo (generic function with 1 method)
julia> foo(n::Number, c::Char) = "$(c)_$(n)"foo (generic function with 2 methods)
julia> @na foo(n=42, r=3.14;)145.14
julia> @na foo(n=42, 3.14;)145.14
julia> @na foo( 42, r=3.14;)145.14
julia> @na foo(n=42, r=3.14; offset=200)245.14

Possible errors

julia> @na (1, 2) # not a function callERROR: LoadError: the expression is not a function call
in expression starting at REPL[1]:1
julia> @na foo(n=42, r=3.14) # missing semicolonERROR: LoadError: ArgumentError: the function call does not have a semicolon. Hints: - The function call should be of the form `f(args...; kwargs...)`. - Use a semicolon even if there are no keyword arguments, e.g. `@na sin(3.14;)`. - You called the macro with the expression `foo(n = 42, r = 3.14)`. in expression starting at REPL[2]:1
julia> @na foo(n=42, "r"=3.14;) # argument name must be a symbolERROR: LoadError: ArgumentError: the argument name should be a symbol, got "r". in expression starting at REPL[3]:1
julia> @na foo(n=42, r='x';) # this calls the second methodERROR: ArgumentError: expected argument name `c`, got `r`.

Arguments and keyword arguments must be correctly separated by the semicolon.

julia> @na foo(n=42, r=3.14, offset=200;) # `offset` is not an argumentERROR: Calling invoke(f, t, args...) would throw:
MethodError: no method matching invoke foo(::Int64, ::Float64, ::Int64)
The function `foo` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  foo(::Integer, ::Real; offset)
   @ Main REPL[2]:1
  foo(::Number, !Matched::Char)
   @ Main REPL[3]:1
julia> @na foo(n=42; r=3.14, offset=200) # `r` is not a keyword argumentERROR: Calling invoke(f, t, args...) would throw: MethodError: no method matching invoke foo(::Int64) The function `foo` exists, but no method is defined for this combination of argument types. Closest candidates are: foo(::Number, !Matched::Char) @ Main REPL[3]:1 foo(::Integer, !Matched::Real; offset) @ Main REPL[2]:1

API

NamedArgs.@naMacro
@na f(arg=val, ...; kwargs)

Allow naming arguments in a function call.

See the module documentation.

Examples

julia> add(x, y; offset=100) = x + y + offset
add (generic function with 1 method)

julia> @na add(x=40, y=2;)
142

julia> @na add(40, y=2; offset=300) # can omit some names
342

julia> @na add(x=4*10, y=2; offset=300) # can use expressions as arguments
342

The following invocations are invalid.

julia> @na add(40, 2) # cannot omit semicolon
ERROR: LoadError: ArgumentError: the function call does not have a semicolon.
[...]

julia> @na add(y=2, x=40;) # cannot swap arguments
ERROR: ArgumentError: expected argument name `x`, got `y`.
[...]

julia> @na add(x=40; y=2, offset=200) # must correctly separate args and kwargs
ERROR: Calling invoke(f, t, args...) would throw:
MethodError: no method matching invoke add(::Int64)
[...]
source