2 bool optional params

spir denis.spir at gmail.com
Wed Nov 10 06:16:05 PST 2010


On Wed, 10 Nov 2010 00:30:55 -0800
Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> On Tuesday 09 November 2010 23:55:26 spir wrote:
> > Hello,
> > 
> > Is there a way for a func to hold 2 optional params of the same type?
> > 	void f(int p, bool b1=false, bool b2=false) {
> > 	    writefln("p=%s b1=%s b2=%s", p,b1,b2);
> > 	 }
> > Or is there a workaroud?
> 
> Try compiling it. It works just fine.
> 
> You should be able to have multiple optional parameters, and their types 
> shouldn't matter. Where you get into trouble is if that function has overloads 
> which conflict. Since, in effect, by declaring
> 
> void f(int p, bool b1 = false, bool b2 = false)
> 
> you've declared
> 
> void f(int p, bool b1, bool b2)
> void f(int p, bool b1)
> void f(int p)

Precisely, what a clear exposure of the issue! Sorry, my question was far to imprecise. I cannot have
	void f(int p, bool b2) {}
If I call it with a single bool param, then D logically maps it to b1. In a language with named params, one would simply write
	f(whatever, b2=true);

Is there any workaround? 
I thought at replacing b2 by a variable external to the func itself. Eg instead
	void test (args, failure=false, silent=false) {}
have
	SILENT_TEST = false;
	void test (args, failure=false) {... also use SILENT_TEST ...}
But I find the solution a bit weird, it breaks lexical scoping, and forces the user to overwrite a variable in the func's original scope (module).
(Is this at all possible:
	import foo;
	foo.K = true;
I'll try...)

> So, trying to declare any of those three separately won't work because your first 
> definition covers them all.

Right, thank you, Jonathan.

> - Jonathan M Davis

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list