www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initializing default parameters from prior parameter

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
Wouldn't it make sense if this would be possible? It would shorten the 
function interface in cases, where no specialization of m is needed but 
still makes it possible to override.

myfunc(myObj o, myType m = o.getmyTypeValue()){...}

The compiler complains, that o is an undefined identifier.

Robert M. Münch
smarter | better | faster
May 25 2019
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 25 May 2019 at 16:25:53 UTC, Robert M. Münch wrote:
 myfunc(myObj o, myType m = o.getmyTypeValue()){...}
I'd probably just write it myType m = null) { if(m is null) m = o.getmyTypeValue; } or myfunc(myObj o) { /* use the default */ } myfunc(myObj o, myType m) { /* use the provided m */ } You might make the first one final if you only want the one overridden.
May 25 2019