# Multiple-choice (dropdown) arguments

**URL:** <https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353>\
**Category:** General Usage\
**Tags:** argument, functions\
**Created:** [April 1, 2021, 10:30am UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353 "2021-04-01T10:30:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RainerHeintzmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rainerheintzmann/32/19726_2.png) [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Post date:** [April 1, 2021, 10:30am UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/1 "2021-04-01T10:30:16Z")

</div>

Writing code in julia, I often encounter the need for an argument which can assume a number of different choices. E.g. in `IndexFunArrays` many functions (e.g. `rr(mysize; offset=CtrFT)`) have the `offset` parameter which can be a tuple or one of the following:  
`(CtrCorner, CtrEnd, CtrFFT, CtrFT, CtrMid, CtrRFFT, CtrRFT)` which are all part of an abstract type `Ctr`.

My question is, whether this is the correct way to go, or wether julia already has a more fitting concept for multiple-choice arguments. For a set of arguments, which appears in multiple places, maybe this mechanism is good, but what about use-cases where this is really only needed for this one function?  
E.g., would it not be nice to write `start_viewer(data::AbstractArray, mode=choice("new", "add", "replace", default="new"))` ?

Does such a mechanism exist? Is it worth writing one?  
Ideally IDEs would support the mechanism, such that the programmer sees the choices when attempting to call the function.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 1, 2021, 12:01pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/2 "2021-04-01T12:01:15Z")

</div>

> [@RainerHeintzmann](#):
>
> Does such a mechanism exist? Is it worth writing one?

I guess it doesn’t, otherwise it would be much, much easier, for example, to choose linetypes, colors, fonts, etc, in plots… particularly if the choices appear by tabbing in the REPL.

I think a good implementation of that would be a huge usability improvement.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [April 1, 2021, 12:38pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/3 "2021-04-01T12:38:21Z")

</div>

This should be doable right? `foo(a,b=\TAB` could simply do a `methods` call, and then return a tree of the available abstract and concrete types. For deep trees I guess recursing through `subtypes` might be expensive? Where is the tab completion code?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 1, 2021, 1:21pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/4 "2021-04-01T13:21:54Z")

</div>

We could go the route of Bash completion. `Base` could define a function called `autocomplete` that takes a function and what is known about the current arguments (maybe in the form of an `Expr`) and suggest things. Then it is responsibility of each module to implement a specific version for their functions, otherwise a generic default is used, that may try to do the best based only on the parameter type annotations available (and maybe the default parameters of optional parameters too).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 1, 2021, 1:25pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/5 "2021-04-01T13:25:49Z")

</div>

> [@rayegun](#):
>
> This should be doable right? `foo(a,b=\TAB` could simply do a `methods` call, and then return a tree of the available abstract and concrete types

I guess the biggest problem is that this will be called from a context in which the type of the already written arguments is not known, i.e., it will be called from the IDE/editor/REPL, the REPL may even know something but there will be context where it does not.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [April 1, 2021, 1:41pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/6 "2021-04-01T13:41:36Z")

</div>

We can ignore the other argument types for most functions I’d think, except for really overloaded functions from Base I don’t imagine it’ll ever be very high. Tab completion only works in the REPL right? I found the completion code I think, and I could take a stab at this. Is `foo(a,b=\TAB` already used somewhere?

But yes that’s a good idea I think on the Base.autocomplete. I’ll give a try prototyping the simple version above tomorrow (ignore other types, just look for that keyword), see if it works, and then give your idea a try.

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [April 1, 2021, 1:53pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/7 "2021-04-01T13:53:22Z")

</div>

Oh but I see what you mean we could give invalid arguments if we don’t look at the others

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [April 1, 2021, 2:09pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/8 "2021-04-01T14:09:56Z")

</div>

Your last example seems like it should probably be handled by an enum. Could then support tab completions for that enum probably.

---

<div class="post-metadata">

**Author:** ![RainerHeintzmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rainerheintzmann/32/19726_2.png) [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Post date:** [April 1, 2021, 2:28pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/9 "2021-04-01T14:28:34Z")

</div>

Yes. Enum or maybe types, with all the subtypes belonging to a common type anchestor  
Something like

```julia
createChoices("Linetype", "dashed", "dotted")

```

yielding the definition of the abstract type `::Linetype <: MultipleChoice` and empty subtypes `::dashed` and `::dotted` .  
A bad thing is that the function would generate types. This sounds more like a macro.

```julia
@choices LineType dashed dotted dot_dashed
@addchoice LineType bold

```

Does this make sense?

---

<div class="post-metadata">

**Author:** ![RainerHeintzmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rainerheintzmann/32/19726_2.png) [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Post date:** [April 1, 2021, 2:34pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/10 "2021-04-01T14:34:15Z")

</div>

… this would avoid invalid arguments, since the specification in the function reads

```julia
function foo(linetype::Linetype)
end

```

---

<div class="post-metadata">

**Author:** ![rayegun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayegun/32/26729_2.png) [@rayegun](https://discourse.julialang.org/u/rayegun)\
**Post date:** [April 1, 2021, 2:46pm UTC](https://discourse.julialang.org/t/multiple-choice-dropdown-arguments/58353/11 "2021-04-01T14:46:56Z")

</div>

I think a plain old enum works best `@enum Line type opt1 opt2`. An empty type could work too I suppose.
