As the argument is ref
, it must have an address. Therefore, it must be an lvalue expression.
You can do this:
JSONValue get(string data, ref int parentCounter = *new int);
You currently can't use this syntax to also give the newly-allocated int
a value. But, in D 2.066, you'll be able to write:
JSONValue get(string data, ref int parentCounter = *new int(10));
This will allocate a new int
on the heap, unless one is specified at the caller site.
You can also use a static variable, or a ref function call:
int defaultValue;
ref int defaultValueFun()
{
auto i = new int;
*i = 10;
return *i;
}
JSONValue get(string data, ref int parentCounter = defaultValue);
// or
JSONValue get(string data, ref int parentCounter = defaultValueFun());
Be wary of this technique if defaultValue
might be called while a reference to its value
is still in use, though.