4 ms·
I've recently played with using nil pointer references as a form of default overloading. The process in the function block was something like: 1) assign defaul
by ben_jones 10y ago
I've recently played with using nil pointer references as a form of default overloading. The process in the function block was something like:
1) assign default value to variable
2) check if pointer parameter is nil
3) if pointer is not nil assign pointer value to variable
4) do work with variable
Am I overlooking a simpler way to do this? I feel like lib.LibraryFuncion(nil, nil) is nicer then lib.LibraryFunction(lib.LibraryDefaultOne, lib.LibraryDefaultTwo), though admittedly the explicitness of the second option is appealing.
- brobinson 10y agoCould you just create well-named functions which call LibraryFunction() internally rather than relying on the caller to specify 1 or more default arguments?
- ben_jones 10y agoYeah. It's funny I'm currently working on two Go libraries one uses the convention you listed [1] and the other [2] uses nil pointers. [1]: https://github.com/b3ntly/go-sms/blob/master/client.go https://github.com/b3ntly/go-sms/blob/master/client.go [2]: https://github.com/b3ntly/mLock/blob/master/lock.go https://github.com/b3ntly/mLock/blob/master/lock.go Disclaimer: Both libraries are in the pre-alpha stages and are extremely light on tests and/or broken.