www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to create a static factory method on a templated

reply Gary Willoughby <dev nomad.so> writes:
I've tried the following code and I get the error:

Error: template Foo(A) does not have property 'of'


struct Foo(A)
{
	private int _foo;

	 disable this();

	public this(int foo)
	{
		this._foo = foo;
	}

	public static auto of(B)()
	{
		return Foo!(B)(8);
	}
}

void main(string[] args)
{
	auto foo = Foo.of!(string);
}


Is it possible to even have static methods on structs like this? 
What am I doing wrong?
Jun 18 2016
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 18 June 2016 at 16:05:53 UTC, Gary Willoughby wrote:
 I've tried the following code and I get the error:
you still have to instantiate you `Foo` here: auto foo = Foo.of!(string); no, you can't call even static methods of *uninstantiated* template. uninstantiated template doesn't exist at all. this works, for example: auto foo = Foo!int.of!(string);
Jun 18 2016