What to do about default function arguments
Martin Nowak
dawg at dawgfoto.de
Thu Apr 26 10:55:05 PDT 2012
On Thu, 26 Apr 2012 06:10:14 +0200, Walter Bright
<newshound2 at digitalmars.com> wrote:
> On 4/25/2012 8:44 PM, Walter Bright wrote:
>> The problem centers around name mangling. If two types mangle the same,
>> then
>> they are the same type. But default arguments are not part of the
>> mangled
>> string. Hence the schizophrenic behavior.
>
> One might suggest mangling the default argument into the type. But
> default arguments need not be compile time constants - they are
> evaluated at runtime! Hence the unattractive specter of trying to mangle
> a runtime expression.
import std.stdio;
int readVal()
{
int val;
stdin.readf("%s", &val);
return val;
}
void main()
{
auto dg = (int a=readVal()) => a;
writeln(dg());
}
----
Stuffing the value into the type is not going to work out when taking the
address.
I think it would be interesting to transform them to values of a type that
preserves the behavior. This would work for polymorphic lambdas as values
too.
----
auto dg = (int a=readVal()) => a;
static struct Lamba
{
int opCall() { return fbody(readVal()); }
int opCall(int a) { return fbody(a); }
int function(int) opAddrOf() { return &fbody; }
static int fbody(int a) { return a; }
}
----
auto dg = a => 2 * a;
struct Lambda
{
auto opCall(Ta)(auto ref Ta a) { return fbody(a); }
@disable opAddrOf();
/*static*/ auto fbody(Ta)(Ta a) { return 2 * a; }
}
More information about the Digitalmars-d
mailing list