# Design advice

**URL:** <https://discourse.julialang.org/t/design-advice/28362>\
**Category:** General Usage\
**Tags:** question, data\_structures\
**Created:** [September 4, 2019, 8:58am UTC](https://discourse.julialang.org/t/design-advice/28362 "2019-09-04T08:58:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 4, 2019, 8:58am UTC](https://discourse.julialang.org/t/design-advice/28362/1 "2019-09-04T08:58:36Z")

</div>

Hi,

I would like to hear some design advice. I want the user to pass two parameters to a function where the second parameter depends on the first one. However, for pratical reason, I dont want the user to write a complicated call. Hence, the actual call is like:

```julia

struct Algo{T}
    a::T
end

# the way that works
function solve(a = 1.0; options = Algo(a))
    # the solve function 
    @show options.a
end

```

whereas I would like it to be

```julia
solve(a = 1.0; options = Algo())

```

but `Algo()` is not defined. Has anyone encountered such question? What have you done?  
Thank you for your suggestions,

Best regards,

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 4, 2019, 9:22am UTC](https://discourse.julialang.org/t/design-advice/28362/2 "2019-09-04T09:22:55Z")

</div>

I don’t understand the question: you want `options` to depend on `a`, but implement it in a way that doesn’t?

I would go with something like

```julia
default_options(a) = A(a) # can customize it later

function solve(a = 1.0; options = default_options(a))
    @show options
end

```

then document and expose `default_options` as part of the API, so if the user wants to customize things the whole thing is clean and transparent.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 4, 2019, 9:34am UTC](https://discourse.julialang.org/t/design-advice/28362/3 "2019-09-04T09:34:50Z")

</div>

> [@rveltz](#):
>
> What have you done?

I wrote `A(a)` instead of `A()`.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 4, 2019, 9:39am UTC](https://discourse.julialang.org/t/design-advice/28362/4 "2019-09-04T09:39:34Z")

</div>

I know it sounds reasonable (altough less if you think that `a` is a struct with 10 fields and `Algo` depends on one of those fields), this is what I proposed. But is seems clunky (he has to pass `a` twice) to me as the user has to know the internals of `A`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 4, 2019, 9:44am UTC](https://discourse.julialang.org/t/design-advice/28362/5 "2019-09-04T09:44:55Z")

</div>

> [@rveltz](#):
>
> you think that `a` is a struct with 10 fields and `Algo` depends on one of those fields),

Something like

```julia
const default_option = ...

function solve(a = 1.0; options = default_option)
     options = Algo(a, options)
end

```

or something then?

You can do whatever you want in `Algo` then and the user only passes in the one field.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 4, 2019, 9:56am UTC](https://discourse.julialang.org/t/design-advice/28362/6 "2019-09-04T09:56:35Z")

</div>

Not sure. My MWE seems too minimal. There are many algorithm (aka `struct`) the user can pass. There is `Algo1`, `Algo2`…

My problem originates from my [package](https://github.com/rveltz/PseudoArcLengthContinuation.jl) where I want the user to pass a linearsolver (ie `a` in the above MWE) and also a Bordered linear solver (ie `Algo1`, `Algo2`… above) which depends on the linear solver passed by the user. But somehow, I was wondering how to make the user pass the linearalgo only once to `solve`.

> Right now, in [package](https://github.com/rveltz/PseudoArcLengthContinuation.jl), the linear bordered solver is pass with a `Symbol`. I would like to remove this and all the `if` in [here](https://github.com/rveltz/PseudoArcLengthContinuation.jl/blob/master/src/LinearBorderSolver.jl#L98).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 4, 2019, 10:07am UTC](https://discourse.julialang.org/t/design-advice/28362/7 "2019-09-04T10:07:06Z")

</div>

> [@rveltz](#):
>
> which depends on the linear solver passed by the user.

This seems to be the crux and is the cause of the repeating. You have `a` both by itself and also shoved in another object. That seems unnecessary. Couldn’t it be something like

```julia
function solve(solver = LinearSolver(); options = Algorithm())
     create_final_object(solver, options)
end

```

That is, the `Algorithm()` object doesn’t need to know about `solver`. But inside the body of the function you are free to mix these however you want.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 4, 2019, 10:09am UTC](https://discourse.julialang.org/t/design-advice/28362/8 "2019-09-04T10:09:49Z")

</div>

Yes I like it! Thank you.
