Default argument values

div0 div0 at users.sourceforge.net
Thu May 20 14:30:28 PDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Adam Ruppe wrote:
> On 5/20/10, bearophile <bearophileHUGS at lycos.com> wrote:
>> A default argument may be an arbitrary expression.
> 
> What I do in code where I want this kind of behaviour is something like this:
> 
> void func(Whatever* options = null) {
>     if(options is null)
>          options = Whatever(default blah blah);
> }
> 
> (substitute null for any invalid amount for the parameter. I recently
> did some timezone functions that used int.min as the default offset,
> meaning load it from the global.)
> 
> If I want to change the Whatever default, I change it there and it
> still works out. Nothing fancy required.
> 
> On 5/20/10, Nick Sabalausky <a at a.a> wrote:
>> I dunno, a caller might rely on a default value being what it was when they
>> had compiled it.
> 
> Eh, if you leave the argument off the list, you're saying "I don't
> really care what this is", so you shouldn't worry about changes... key
> word being "shouldn't". I know I've broken this rule before, so having
> to recompile at least would be nice, like you say.

Yeah, I've gone off default arguments, there is rarely a defensibly good
default argument, and you can achieve exactly the same functionality
with overloads.

C# dropped them, and when I do c# I don't miss default args.

Either way of doing them sucks; better just to have a properly
documented interface.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFL9an0T9LetA9XoXwRAla5AKCXEizxgvbcNHVWwn+HgRRtukC8RACeJEFc
7wUACeT+f3WGTOLZpBGNcys=
=unAD
-----END PGP SIGNATURE-----


More information about the Digitalmars-d mailing list