On Sat, Apr 19, 2014 at 1:52 PM, Justin Israel wrote:
I was wondering what the proper idiom would be for wrapping C++ functions
that contain a number of optional parameters. For instance:

bool write (const std::string &filename,
const std::string &fileformat = std::string(),
ProgressCallback progress callback=NULL)

Is it preferable to expose this in Go as a single function, where the user
would always have to pass the full set of values:

func Write(filename, fileformat string, progress ProgressCallback) bool

Write("foo.jpg", "", nil)
Write("foo", "jpg", nil)
Write("foo", "jpg", myCallback)

Or should it be broken out into explicit Write* calls:

func Write(filename string) bool
func WriteFormat(filename, fileformat string) bool
func WriteFormatCallback(filename, fileformat string, progress
ProgressCallback) bool

This is a smaller example. There are functions that contain 1 required and 4
optional. So I could end up with a number of variants of the call vs one
where they have to pass in at least zero value params.
There is no one answer. Optional parameters are a form of function
overloading. Go encourages people to choose good names for different
versions of functions, rather than overloading a single name with
different meanings. So one approach is to think of the different ways
that a function is used, and choose good names. Or, if optional
parameters are there simply to provide options with typical defaults,
perhaps an Options struct would be appropriate.


You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 5 | next ›
Discussion Overview
groupgolang-nuts @
postedApr 19, '14 at 8:52p
activeApr 21, '14 at 11:08a



site design / logo © 2021 Grokbase