Private default arguments?

Nick Treleaven ntrel-public at yahoo.co.uk
Fri Dec 28 05:42:05 PST 2012


On 28/12/2012 10:58, bearophile wrote:
> An alternative idea (that I maybe I proposed years ago in a weaker form)
> is to introduce 'private' default arguments (they must have a default
> value):
>
>
> void radixSort3(uint[] items, private in uint shiftBits=24) {
>      ...
>      if (shiftBits > 0) {
>          ...
>          radixSort(array[...], shiftBits - 8);
>      }
> }

I like the idea, it's intuitive.

> The 'private' means that only radixSort3 is allowed to set a shiftBits
> argument value.

I think the feature is more useful/flexible if private still means 
module visibility. I realize you would lose the benefit when using 
radixSort3 from the same module, but then the feature has more potential 
applications. (Ideally D would have both module-private and 
local-private qualifiers, but that's another topic).

The feature might be useful for things like logging:

void log(T...)(T args, private string file = __FILE__,
	private size_t line = __LINE__);

That could allow the compiler to distinguish variadic arguments from 
non-overridable default arguments (for calls outside private scope).

It's also nice to prevent the user accidentally overriding file and 
line. To allow wrapping log(), another function can be provided, without 
any default arguments:

void logWrapped(string s, string file, size_t line);


More information about the Digitalmars-d mailing list