:assoc-fn and :default do not really play nice together

Description

Consider an option that can have zero or more values so we store it in a vector (or set). The obvious approach is to specify the empty vector (or set) in :default and use :assoc-fn to conj additional values into it. However, if you want the option to have a non-empty default, if you specify that for :default then :assoc-fn adds the extra values in and you end up with both the default AND the user-supplied values.

What you really want is for the default to only be applied if nothing was assoc'd in.

Currently, there's history that an incrementing argument should have a :default of 0 and then :assoc-fn can be (fn [m k _] (update m k inc)) – and that example is given specifically.

The assumption is that every option starts with the :default value (if given) and the other functions are called as each argument comes in but there's no way to provide a starting value for the :assoc-fn separate from the :default value for the option.

I'm not sure what the best approach here would be.

Environment

None

Activity

Show:

import September 27, 2018 at 11:30 PM

Comment made by: dorab

Thanks much.

Sean Corfield September 25, 2018 at 6:06 AM

I just pushed some updates to both the README and the docstring for parse-opts.

import September 24, 2018 at 7:06 PM

Comment made by: dorab

OK. Fair enough.
Thanks.

Sean Corfield September 24, 2018 at 7:03 PM

The docstring for parse-opts is already huge – I can't put all the docs in it!

import September 24, 2018 at 6:55 PM

Comment made by: dorab

Thanks.
Sorry, I though I read all the docs first, but obviously not.

That Example Usage section on :default vs {:default-fn}} describes the process well.
It might be good to add that to the parse-opts docstring as well.

Completed

Details

Assignee

Reporter

Priority

Created July 20, 2018 at 9:51 PM
Updated September 27, 2018 at 11:30 PM
Resolved September 27, 2018 at 11:30 PM