Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Optional arguments+splatting

$
0
0

Julia’s creators say they are greedy - so am I. I want to create a function g that accepts a list of arguments (I am trying with a named tuple), and passes it on to a function f. I have 3 requirements

  • The user of g must provide “named arguments” as in “a=3”, for readability of the code calling g
  • Arguments must be optional, with fallback on a default value
  • and here comes the crunch: g must not be concerned with the details of this argument list.

Why would I ever want to do that? Well, in the real world, g does a bit of work behind the scenes.

I try

struct T
    a
    b
end
f(;a=0,b=0) = T(a,b)
g(foo,s...) = foo(s...)
r=g(f,(a=2))

but splatting was clearly not intended for named tuples. Any ideas that do not involve metaprogramming? :grinning:

I am knocking off for the day, but I am wondering if g could become a macro… metaprogramming-light.

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles