D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3430 - Cannot use constructor in member function default argument
Summary: Cannot use constructor in member function default argument
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2009-10-21 00:19 UTC by Don
Modified: 2019-10-10 13:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2009-10-21 00:19:22 UTC
class Foo{
   this(int a){}
  static void bug(Foo a = new Foo(77)){}
}
test.d(4): Error: no constructor for Foo

This has been present since dinosaurs walked the earth(also failed in DMD 0.175).
Moving the function outside the class allows it to compile.

The struct version is D2 only:

struct Foo{
   this(int a){}
   void bug(Foo a = Foo(77)){}
}

test.d(4): Error: more initializers than fields of Foo

This is part of a family of closely related bugs.
If instead, the function is in a different struct/class, you get ICE bug 2437;
if it's a free function, you get ICE bug 2935.
Comment 1 RazvanN 2019-10-10 13:23:35 UTC
This code successfully compiles in git master.