Function pointers/delegates default args were stealth removed?

Walter Bright newshound2 at digitalmars.com
Mon Aug 27 13:53:50 PDT 2012


On 8/27/2012 6:05 AM, Timon Gehr wrote:
> On 08/27/2012 02:48 PM, Robert Clipsham wrote:
>> I seem to recall I looked at this issue myself at one point. It goes
>> something like:
>> ----
>> auto foo = (int a = 1) { return a; };
>> auto bar = (int a) { return a; };
>> ----
>> int function(int) is mangled exactly the same as int function(int = 1)
>> as default args aren't used for mangling. dmd does semantic analysis on
>> the type of foo, which returns int function(int = 1), which is mangled
>> as int function(int) and stored in dmd's hashmap of types (default args
>> aren't mangled). When the semantic analysis of bar is done it checks the
>> hashmap, sees that the type is already there (has the same name
>> mangling) and does not repeat semantic analysis. If you switch the order
>> of declarations then the opposite happens - the default arg is ignored.

This is correct.

> If the compiler design *requires* equal types to be stored uniquely in
> a hash map, then the default args shouldn't be stored as part of the
> type in the AST. I assume it is rather inconvenient to fix, but
> certainly possible.

The language design requires a 1:1 mapping of mangling to types. Hence the 
compiler design to use the mangling as a hashmap key of types. The failure of 
that approach in this case points to a problem in the language design, not a bug 
in the compiler.


More information about the Digitalmars-d mailing list