www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicit constructor call

reply LeoD <leo.dahlmann gmail.com> writes:
Hi,
can constructors be called implicit in D? Example:

class Foo
{
	this(int i)
	{

	}
}

void test(Foo f)
{

}

int main()
{
	test(100); // Here Foo.this(100); should get called

	return 0;
}


And why are there no templated constructors in D? Example:

this(T)(T parameter)
{

}
Jan 12 2007
next sibling parent Marcin Kuszczak <aarti interia.pl> writes:
LeoD wrote:

 Hi,
 can constructors be called implicit in D? Example:
 
 class Foo
 {
 this(int i)
 {
 
 }
 }
 
 void test(Foo f)
 {
 
 }
 
 int main()
 {
 test(100); // Here Foo.this(100); should get called
 
 return 0;
 }
 
No. But I agree that it could be nice addition to D.
 
 And why are there no templated constructors in D? Example:
 
 this(T)(T parameter)
 {
 
 }
Same here. They are not templated in D, but would be nice if they will... Hopefully Walter will add above features in future releases of DMD... -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus - http://zapytaj.dlajezusa.pl (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Jan 12 2007
prev sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
LeoD wrote:
 Hi,
 can constructors be called implicit in D? Example:
 
 class Foo
 {
 	this(int i)
 	{
 
 	}
 }
 
 void test(Foo f)
 {
 
 }
 
 int main()
 {
 	test(100); // Here Foo.this(100); should get called
 
 	return 0;
 }
You can get this using typesafe variadic arguments: See: http://digitalmars.com/d/function.html Heading: Variadic Functions Subheading: Typesafe Variadic Functions
 
 And why are there no templated constructors in D? Example:
 
 this(T)(T parameter)
 {
 
 }
Because constructors are not virtual... or at least that's the explanation given in compiler errors with this. Seems more like just a special case. I was actually a little bit disappointed myself, though the use cases seem to be pretty niche. -- Chris Nicholson-Sauls
Jan 12 2007
parent Marcin Kuszczak <aarti interia.pl> writes:
Chris Nicholson-Sauls wrote:

 LeoD wrote:
 Hi,
 can constructors be called implicit in D? Example:
You can get this using typesafe variadic arguments: See: http://digitalmars.com/d/function.html Heading: Variadic Functions Subheading: Typesafe Variadic Functions
Interesting. I didn't know that. Could be useful, but to say true I think that syntax/semantic is really strange...
 
 And why are there no templated constructors in D? Example:
 
 
 Because constructors are not virtual... or at least that's the explanation
 given in
 compiler errors with this.  Seems more like just a special case.  I was
 actually a little bit disappointed myself, though the use cases seem to be
 pretty niche.
As I know constructors in C++ are also not virtual, but they can be templated. I don't get connection here... It could be useful for variant classes which could be constructed with different types. I couldn't translate Boost::Any well because of that problem (there is enhancement request on bugzilla). -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus - http://zapytaj.dlajezusa.pl (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Jan 12 2007