Performance implications of computations in keyword arguments

loisel
Sat, 30 Aug 2025 20:05:39 +0000
New to Julia

Long story short, I’m putting my entire function into default values of keyword arguments, and I want to know if this is bad.

I’m working on a Julia package to solve PDEs. The “driver code” of such solvers often has a zoo of options/customization. I’ve taken to using a certain unusual design pattern and I’m wondering if there are any implications in terms of the performance, precompilation or whatever. Let’s say that you want to make a function that computes the first few terms of the Fibonacci sequence.

function fib(;x0 = 1,
    x1 = 1,
    x2 = x0+x1,
    x3 = x1+x2,
    x4 = x2+x3,
    x5 = x3+x4)
    return [x0,x1,x2,x3,x4,x5]
end

So now I can do fib() to get a default behavior, or fib(;x2=5) to get some strange variant. I can also cascade these functions to provide increasing levels of complexity:

fib_variant(;args...) = fib(x3=9;args...)

In other words, the body of my function is almost nothing and most of my computation is done in keyword parameters. This way, people can start using my code without having to understand all the parameters, just by doing fib(), and as they get deeper into whatever problem, they can modify the behavior of my function in very general ways.

Is this bad in some way?

S

10 posts - 5 participants