How to check at compile time if Default Constructor for a class exists?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 1 19:30:09 PST 2016


On Saturday, 2 January 2016 at 02:42:56 UTC, Bottled Gin wrote:
>> It seems like saying that a constructor with all default 
>> arguments is not a default constructor is a distinction 
>> without meaning. What are you doing that you would care?
>>
>
> The Object.factory does not seem to consider a constructor with 
> default arguments as a defaultConstructor. Consider the 
> following code snippet. It works only when I uncomment line 3.
>
> module foo;                               // 1
> class Foo {				  // 2
>   // this() {}				  // 3
>   this(int n=0) {}		          // 4
> }					  // 5
> void main() {				  // 6
>   auto obj = Object.factory("foo.Foo");	  // 7
>   assert(obj !is null);			  // 8
>   auto foo = cast(Foo) obj;		  // 9
>   assert(foo !is null);			  // 10
> }					  // 11

Well, I would definitely consider it a bug for the Object factory 
to not work with a constructor with all default arguments, though 
given that the default arguments are known at compile time and 
not runtime, I don't know if it can actually treat a constructor 
with all default arguments like a default constructor or not. 
That would depend on how it works, and I'm not familiar with its 
inner workings. So, you may have found a place where the 
distinction between a constructor with no parameters and one with 
all default arguments does matter much as it really shouldn't.

- Jonathan M Davis


More information about the Digitalmars-d mailing list