Private default arguments?

Maxim Fomin maxim at maxim-fomin.ru
Fri Dec 28 07:29:58 PST 2012


On Friday, 28 December 2012 at 10:58:59 UTC, bearophile wrote:
> Often recursive functions need some "bookkeeping" default 
> arguments that aren't meant to be used by the user, they are 
> meant to be used only by recursive calls:

What happens when function with private argument is stored in a 
function pointer? Currently (2.061 beta) if you do so with a 
function with default argument, you have to supply all default 
arguments (it seems that some information is lost when making 
function pointer from function). So, declaring some default 
argument as private makes function pointer useless: on the one 
hand, you have to supply all arguments when calling from pointer, 
on the other hand it is forbidden to supply default private 
arguments.

I don't like a name for this feature because it breaks logic that 
private is accessible within module. Perhaps a new name?

I also against some simple and dummy constraints (like 
one-path-constructor call restriction) which can be easily 
avoided and does not really prevent from making mistakes.

class A
{
	this(int i) { }
	this()
	{
		int x;
		// it is rejected
		// Error: one path skips constructor
		// Error: return without calling constructor
		// if (x)
		//	this(1);
		if (x) // it works
			__ctor(1);
	}
}

void main()
{
	A a = new A;
}

I think your private argument constraint can be as easily broken: 
D is a system language and there is no way to prevent it from 
calling arbitrary code. IMHO it does make sense to apply 
restrictions which prevent from doing danger things - this 
reduces space for mistakes, but constraints like you propose and 
above are not that serious as others.


More information about the Digitalmars-d mailing list