#Most convenient way to include a single optional parameter in a call out of order.

17 messages · Page 1 of 1 (latest)

dreamy tree
#

Let's say I'm calling an API with a bunch of optional parameters that looks like

EventSource::GetSource(const string& database, std::vector<uint32> stream_ids, const Time start, const Time end, const uint128 start_id = 0, const uint128 end_id = Uint128Max(), const bool offline_access = false, string_view identity = "", const uint64 read_timestamp = 0);
``` If the thing I'd like to call it with is the minimum number of required parameters (database, stream_ids, start, end) plus the very last optional parameter (read_timestamp), what's the idiomatic way to do this? Let's say refactoring the order of the API parameters isn't an option.

Obviously I could just pass in the defaults manually: ```cpp
GetSource(my_database, my_stream_ids, my_start, my_end, /*start_id=*/0, /*end_id=*/Uint128Max(), /*offline_access=*/false, /*identity=*/"", my_read_timestamp)
``` but this runs into the problem that if the API's defaults change, my function has to change too or be out of date (and besides, it's ugly). I know in Python I could just do ```py
GetSource(my_database, my_stream_ids, my_start, my_end, read_timestamp=my_read_timestamp
``` Is there an equivalent syntax or preferred pattern in C++?
manic tapirBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

rancid knoll
#

this is a bit of a conundrum in basically any language

#

default param vals are convenient, until they're not

#

meaning, until you arrive at the situation you face, which is that you have 10 params, but you don't want to bother specifying, say, the middle 3 of them

#

now, add to this that if you don't control the API, and the API only has this type of interface, you're half screwed

#

but, as far as ways to resolve this in, again, most any language, one of the main ways is to pass what is effectively a hash map, or what Python calls dict and what JS calls object

#

or more contextual, what C++ calls unordered_map

#

but of course, that means you have to write a wrapper that takes such, and generates default values for items that don't exist in the map

#

and yes that means your default values may go out of sync with the defaults in the lib you're using

#

if you still feel your defaults are sane/reasonable then there's not much worry (and they should remain mostly so if the lib in question adheres to semantic versioning)

#

**kwargs is in fact just a dict by the time you get to it

dreamy tree
#

Gotcha, makes sense. Will probably just manually pass in the defaults, then. Thanks for the look!

#

!solved

manic tapirBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] Most convenient way to include a single optional parameter in a call out of order.

manic tapirBOT
#

@dreamy tree

This question thread is being automatically marked as solved.