www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Operator overloading

reply "Xan" <xancorreu gmail.com> writes:
Hi, I read http://dlang.org/operatoroverloading.html but in my 
code it does not work. I tried to overload '*' binary operator in 
my class Algorisme:

[...]
class Algorisme(U,V) {
	string nom;
	uint versio;
	alias V function (U) Funcio;
	Funcio funcio;

	this(string nom, uint versio, Funcio funcio) {
		try {
			this.nom = nom;
			this.versio = versio;
			this.funcio = funcio;
		}
		catch {
			writeln("Error");
		}
	}

	string toString() {
		return format("%s (versió %s): %s -> %s", nom, versio, 
typeid(U), typeid(V));
	}

	Algorisme(U, V) opBinary(string op) (Algorisme(U, V) alg) {
		static if (op == '*') return new Algorisme(U,V)("composició", 
this.versio+alg.versio, this.funcio);
	}

}
[...]

but I receive these errors:

$ gdmd-4.6 algorisme.d
algorisme.d:31: function declaration without return type. (Note 
that constructors are always named 'this')
algorisme.d:31: no identifier for declarator Algorisme(U, V)
algorisme.d:31: semicolon expected following function declaration
algorisme.d:31: function declaration without return type. (Note 
that constructors are always named 'this')
algorisme.d:31: function declaration without return type. (Note 
that constructors are always named 'this')
algorisme.d:31: found 'alg' when expecting ')'
algorisme.d:31: no identifier for declarator 
opBinary(Algorisme(U, V))
algorisme.d:31: semicolon expected following function declaration
algorisme.d:31: Declaration expected, not ')'
algorisme.d:35: unrecognized declaration


Why it fails?
Anyone could help me?

Thanks,
Xan.
Apr 19 2012
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 19.04.2012 23:14, Xan wrote:
 Hi, I read http://dlang.org/operatoroverloading.html but in my code it
 does not work. I tried to overload '*' binary operator in my class
 Algorisme:

 [...]
 class Algorisme(U,V) {
 string nom;
 uint versio;
 alias V function (U) Funcio;
 Funcio funcio;

 this(string nom, uint versio, Funcio funcio) {
 try {
 this.nom = nom;
 this.versio = versio;
 this.funcio = funcio;
 }
 catch {
 writeln("Error");
 }
 }

 string toString() {
 return format("%s (versió %s): %s -> %s", nom, versio, typeid(U),
 typeid(V));
 }

 Algorisme(U, V) opBinary(string op) (Algorisme(U, V) alg) {
Algorisme! opBinary(string op) (Algorisme alg) { or Algorisme!(U,V) opBinary(string op) (Algorisme!(U,V) alg) { should do it static if (op == '*') return new Algorisme("composició", or: static if (op == '*') return new Algorisme!(U,v)("composició", same here. There is no need to put !(params) explicitly if it's the same as the template you are writing.
 this.versio+alg.versio, this.funcio);
 }

 }
 [...]

 but I receive these errors:

 $ gdmd-4.6 algorisme.d
 algorisme.d:31: function declaration without return type. (Note that
 constructors are always named 'this')
 algorisme.d:31: no identifier for declarator Algorisme(U, V)
 algorisme.d:31: semicolon expected following function declaration
 algorisme.d:31: function declaration without return type. (Note that
 constructors are always named 'this')
 algorisme.d:31: function declaration without return type. (Note that
 constructors are always named 'this')
 algorisme.d:31: found 'alg' when expecting ')'
 algorisme.d:31: no identifier for declarator opBinary(Algorisme(U, V))
 algorisme.d:31: semicolon expected following function declaration
 algorisme.d:31: Declaration expected, not ')'
 algorisme.d:35: unrecognized declaration


 Why it fails?
 Anyone could help me?

 Thanks,
 Xan.
-- Dmitry Olshansky
Apr 19 2012
parent "Xan" <xancorreu gmail.com> writes:
On Thursday, 19 April 2012 at 19:24:40 UTC, Dmitry Olshansky 
wrote:
 On 19.04.2012 23:14, Xan wrote:
 Hi, I read http://dlang.org/operatoroverloading.html but in my 
 code it
 does not work. I tried to overload '*' binary operator in my 
 class
 Algorisme:

 [...]
 class Algorisme(U,V) {
 string nom;
 uint versio;
 alias V function (U) Funcio;
 Funcio funcio;

 this(string nom, uint versio, Funcio funcio) {
 try {
 this.nom = nom;
 this.versio = versio;
 this.funcio = funcio;
 }
 catch {
 writeln("Error");
 }
 }

 string toString() {
 return format("%s (versió %s): %s -> %s", nom, versio, 
 typeid(U),
 typeid(V));
 }

 Algorisme(U, V) opBinary(string op) (Algorisme(U, V) alg) {
Algorisme! opBinary(string op) (Algorisme alg) { or Algorisme!(U,V) opBinary(string op) (Algorisme!(U,V) alg) { should do it static if (op == '*') return new Algorisme("composició", or: static if (op == '*') return new Algorisme!(U,v)("composició", same here. There is no need to put !(params) explicitly if it's the same as the template you are writing.
 this.versio+alg.versio, this.funcio);
 }
Thanks, Dmitry, but it's a correction "*" instead of '*' (string instead of char) The definitive code is: Algorisme!(U,V) opBinary(string op)(Algorisme!(U,V) alg) { static if (op=="*") return new Algorisme!(U,V)("composició", this.versio+alg.versio, this.funcio); } Thanks a lot, another time, Xan.
 }
 [...]

 but I receive these errors:

 $ gdmd-4.6 algorisme.d
 algorisme.d:31: function declaration without return type. 
 (Note that
 constructors are always named 'this')
 algorisme.d:31: no identifier for declarator Algorisme(U, V)
 algorisme.d:31: semicolon expected following function 
 declaration
 algorisme.d:31: function declaration without return type. 
 (Note that
 constructors are always named 'this')
 algorisme.d:31: function declaration without return type. 
 (Note that
 constructors are always named 'this')
 algorisme.d:31: found 'alg' when expecting ')'
 algorisme.d:31: no identifier for declarator 
 opBinary(Algorisme(U, V))
 algorisme.d:31: semicolon expected following function 
 declaration
 algorisme.d:31: Declaration expected, not ')'
 algorisme.d:35: unrecognized declaration


 Why it fails?
 Anyone could help me?

 Thanks,
 Xan.
Apr 19 2012
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, April 19, 2012 21:14:43 Xan wrote:
 Hi, I read http://dlang.org/operatoroverloading.html but in my
 code it does not work. I tried to overload '*' binary operator in
 my class Algorisme:
 
 [...]
 class Algorisme(U,V) {
 string nom;
 uint versio;
 alias V function (U) Funcio;
 Funcio funcio;
 
 this(string nom, uint versio, Funcio funcio) {
 try {
 this.nom = nom;
 this.versio = versio;
 this.funcio = funcio;
 }
 catch {
 writeln("Error");
 }
 }
 
 string toString() {
 return format("%s (versió %s): %s -> %s", nom, versio,
 typeid(U), typeid(V));
 }
 
 Algorisme(U, V) opBinary(string op) (Algorisme(U, V) alg) {
 static if (op == '*') return new Algorisme(U,V)("composició",
 this.versio+alg.versio, this.funcio);
 }
 
 }
 [...]
 
 but I receive these errors:
 
 $ gdmd-4.6 algorisme.d
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: no identifier for declarator Algorisme(U, V)
 algorisme.d:31: semicolon expected following function declaration
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: found 'alg' when expecting ')'
 algorisme.d:31: no identifier for declarator
 opBinary(Algorisme(U, V))
 algorisme.d:31: semicolon expected following function declaration
 algorisme.d:31: Declaration expected, not ')'
 algorisme.d:35: unrecognized declaration
 
 
 Why it fails?
 Anyone could help me?
Use a template constraint rather than a static if. As it stands, any operator other than "*" will result in a function with no return statement. Algorisme opBinary(string op)(Algorisme alg) if(op == "*") { return new Algorisme("composició", this.versio+alg.versio, this.funcio); } - Jonathan M Davis
Apr 19 2012
parent reply "Xan" <xancorreu gmail.com> writes:
On Thursday, 19 April 2012 at 20:59:05 UTC, Jonathan M Davis 
wrote:
 On Thursday, April 19, 2012 21:14:43 Xan wrote:
 Hi, I read http://dlang.org/operatoroverloading.html but in my
 code it does not work. I tried to overload '*' binary operator 
 in
 my class Algorisme:
 
 [...]
 class Algorisme(U,V) {
 string nom;
 uint versio;
 alias V function (U) Funcio;
 Funcio funcio;
 
 this(string nom, uint versio, Funcio funcio) {
 try {
 this.nom = nom;
 this.versio = versio;
 this.funcio = funcio;
 }
 catch {
 writeln("Error");
 }
 }
 
 string toString() {
 return format("%s (versió %s): %s -> %s", nom, versio,
 typeid(U), typeid(V));
 }
 
 Algorisme(U, V) opBinary(string op) (Algorisme(U, V) alg) {
 static if (op == '*') return new Algorisme(U,V)("composició",
 this.versio+alg.versio, this.funcio);
 }
 
 }
 [...]
 
 but I receive these errors:
 
 $ gdmd-4.6 algorisme.d
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: no identifier for declarator Algorisme(U, V)
 algorisme.d:31: semicolon expected following function 
 declaration
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: function declaration without return type. (Note
 that constructors are always named 'this')
 algorisme.d:31: found 'alg' when expecting ')'
 algorisme.d:31: no identifier for declarator
 opBinary(Algorisme(U, V))
 algorisme.d:31: semicolon expected following function 
 declaration
 algorisme.d:31: Declaration expected, not ')'
 algorisme.d:35: unrecognized declaration
 
 
 Why it fails?
 Anyone could help me?
Use a template constraint rather than a static if. As it stands, any operator other than "*" will result in a function with no return statement. Algorisme opBinary(string op)(Algorisme alg) if(op == "*") { return new Algorisme("composició", this.versio+alg.versio, this.funcio); } - Jonathan M Davis
Thanks, Jonathan. I suppose with 'if' (dynamic), it generates Exception if we call with other operator than '*', isn't? Thanks, Xan.
Apr 20 2012
next sibling parent reply "Xan" <xancorreu gmail.com> writes:
What fails if I want to define this:

	Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
		if (op=="*") {
			//fer la funció composicio
			return new Algorisme!(T,V)("Composició de "~this.nom ~ " i " 
~ alg.nom, 1, function(T t) { return this.funcio(alg.funcio(t)); 
} );
		}
	}

}


within my
class Algorisme(U,V) {
	string nom;
	uint versio;
	alias V function (U) Funcio;
	Funcio funcio;
...
}

?


I want to combine Algorisme(U,V) and Algorisme(T,V) with 
operator. Is it possible?

The errors I get are:

  gdmd-4.6 algorisme
algorisme.d:32: Error: undefined identifier T, did you mean 
variable E?
algorisme.d:51: Error: 'alg' is not of arithmetic type, it is a 
algorisme.Algorisme!(int,int).Algorisme
algorisme.d:51: Error: 'alg2' is not of arithmetic type, it is a 
algorisme.Algorisme!(int,int).Algorisme



Thanks in advance,
Xan.
Apr 20 2012
next sibling parent "Xan" <xancorreu gmail.com> writes:
On Friday, 20 April 2012 at 14:10:31 UTC, Xan wrote:
 What fails if I want to define this:

 	Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
 		if (op=="*") {
 			//fer la funció composicio
 			return new Algorisme!(T,V)("Composició de "~this.nom ~ " i 
 " ~ alg.nom, 1, function(T t) { return 
 this.funcio(alg.funcio(t)); } );
 		}
 	}

 }


 within my
 class Algorisme(U,V) {
 	string nom;
 	uint versio;
 	alias V function (U) Funcio;
 	Funcio funcio;
 ...
 }

 ?


 I want to combine Algorisme(U,V) and Algorisme(T,V) with 
 operator. Is it possible?

 The errors I get are:

  gdmd-4.6 algorisme
 algorisme.d:32: Error: undefined identifier T, did you mean 
 variable E?
 algorisme.d:51: Error: 'alg' is not of arithmetic type, it is a 
 algorisme.Algorisme!(int,int).Algorisme
 algorisme.d:51: Error: 'alg2' is not of arithmetic type, it is 
 a algorisme.Algorisme!(int,int).Algorisme



 Thanks in advance,
 Xan.
The full code is here: https://gist.github.com/2429005 (I put the code out of the class and I drop one error)
Apr 20 2012
prev sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 20.04.2012 18:10, Xan wrote:
 What fails if I want to define this:

 Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
Algorisme!(T,V) opBinary(string op, T)(Algorisme!(T,U) alg) { You need to name what T is and that is *sometype*. Anyway I suggest getting a decent book (TDPL).
 if (op=="*") {
static if is conceptually and technically better here. -- Dmitry Olshansky
Apr 20 2012
parent reply "Xan" <xancorreu gmail.com> writes:
On Friday, 20 April 2012 at 14:18:37 UTC, Dmitry Olshansky wrote:
 On 20.04.2012 18:10, Xan wrote:
 What fails if I want to define this:

 Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
Algorisme!(T,V) opBinary(string op, T)(Algorisme!(T,U) alg) { You need to name what T is and that is *sometype*. Anyway I suggest getting a decent book (TDPL).
 if (op=="*") {
static if is conceptually and technically better here.
Thanks, Dmitry, for your suggestions, but it does not work too: $ gdmd-4.6 algorisme algorisme.d:54: Error: 'alg' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme algorisme.d:54: Error: 'alg2' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme I update de gist: https://gist.github.com/2429005 By the other hand, is there any way to put the definition of operator * in the class (not out like I have now)? Thanks, Xan.
Apr 20 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 20.04.2012 19:14, Xan wrote:
 On Friday, 20 April 2012 at 14:18:37 UTC, Dmitry Olshansky wrote:
 On 20.04.2012 18:10, Xan wrote:
 What fails if I want to define this:

 Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
Algorisme!(T,V) opBinary(string op, T)(Algorisme!(T,U) alg) { You need to name what T is and that is *sometype*. Anyway I suggest getting a decent book (TDPL).
 if (op=="*") {
static if is conceptually and technically better here.
Thanks, Dmitry, for your suggestions, but it does not work too: $ gdmd-4.6 algorisme algorisme.d:54: Error: 'alg' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme algorisme.d:54: Error: 'alg2' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme I update de gist: https://gist.github.com/2429005 By the other hand, is there any way to put the definition of operator * in the class (not out like I have now)?
it should be inside the class. Why would you put it outside and hope it work? What 'this' can refer to in free function? Please read spec at the very least. http://dlang.org/operatoroverloading.html
 Thanks,
 Xan.
-- Dmitry Olshansky
Apr 20 2012
parent reply "Xan" <xancorreu gmail.com> writes:
On Friday, 20 April 2012 at 18:47:14 UTC, Dmitry Olshansky wrote:
 On 20.04.2012 19:14, Xan wrote:
 On Friday, 20 April 2012 at 14:18:37 UTC, Dmitry Olshansky 
 wrote:
 On 20.04.2012 18:10, Xan wrote:
 What fails if I want to define this:

 Algorisme!(T,V) opBinary(string op)(Algorisme!(T,U) alg) {
Algorisme!(T,V) opBinary(string op, T)(Algorisme!(T,U) alg) { You need to name what T is and that is *sometype*. Anyway I suggest getting a decent book (TDPL).
 if (op=="*") {
static if is conceptually and technically better here.
Thanks, Dmitry, for your suggestions, but it does not work too: $ gdmd-4.6 algorisme algorisme.d:54: Error: 'alg' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme algorisme.d:54: Error: 'alg2' is not of arithmetic type, it is a algorisme.Algorisme!(int,int).Algorisme I update de gist: https://gist.github.com/2429005 By the other hand, is there any way to put the definition of operator * in the class (not out like I have now)?
it should be inside the class. Why would you put it outside and hope it work? What 'this' can refer to in free function?
Yes, you're wright. So in. But what fails? I reveice these errors and I have no idea what fails!
 Please read spec at the very least.
 http://dlang.org/operatoroverloading.html
I read it. It served me as guide. But not I'm in trouble.... Can you help me fixing the bug! Regards, Xan.
 Thanks,
 Xan.
Apr 20 2012
parent reply "Xan" <xancorreu gmail.com> writes:
 Yes, you're wright. So in.

 But what fails?

 I reveice these errors and I have no idea what fails!
Sorry, the errors are: $ gdmd-4.6 algorisme.d algorisme.d:35: Error: 'this' is only defined in non-static member functions, not __funcliteral1 algorisme.d:35: Error: function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opB nary.__funcliteral1 cannot access frame of function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary algorisme.d:35: Error: function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opB nary.__funcliteral1 cannot access frame of function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary algorisme.d:35: Error: function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opB nary.__funcliteral1 cannot access frame of function algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary algorisme.d:35: Error: constructor algorisme.Algorisme!(int,int).Algorisme.this (string nom, uint versio, int function(int) funcio) is not callable using argument types (string,int,_error_ function(int t) nothrow system) algorisme.d:35: Error: cannot implicitly convert expression (__funcliteral1) of type _error_ function(int t) nothrow system to int function(int) algorisme.d:52: Error: template instance algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int) error instantiating I update the gist: https://gist.github.com/2429005
Apr 20 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 20.04.2012 23:33, Xan wrote:
 Yes, you're wright. So in.

 But what fails?

 I reveice these errors and I have no idea what fails!
Sorry, the errors are: $ gdmd-4.6 algorisme.d algorisme.d:35: Error: 'this' is only defined in non-static member functions, not __funcliteral1
Easy - _this_ inside of function (T t) { return this.funcio(alg.funcio(t)); } Is not going to fly because there is no way to "store" _this_ in function, use delegates (this is a context and it has to be "copied" -> use delegates). But then you'd have to change to delegate everywhere. (and there is toDelegate function in std.functional, that converts function to delegate)
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: constructor
 algorisme.Algorisme!(int,int).Algorisme.this (string nom, uint versio,
 int function(int) funcio) is not callable using argument types
 (string,int,_error_ function(int t) nothrow  system)
 algorisme.d:35: Error: cannot implicitly convert expression
 (__funcliteral1) of type _error_ function(int t) nothrow  system to int
 function(int)
 algorisme.d:52: Error: template instance
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int) error
 instantiating



 I update the gist: https://gist.github.com/2429005
-- Dmitry Olshansky
Apr 20 2012
parent "Xan" <xancorreu gmail.com> writes:
Thank you very very much, Dmitry. Now all it's fine!

Xan.

On Friday, 20 April 2012 at 19:51:40 UTC, Dmitry Olshansky wrote:
 On 20.04.2012 23:33, Xan wrote:
 Yes, you're wright. So in.

 But what fails?

 I reveice these errors and I have no idea what fails!
Sorry, the errors are: $ gdmd-4.6 algorisme.d algorisme.d:35: Error: 'this' is only defined in non-static member functions, not __funcliteral1
Easy - _this_ inside of function (T t) { return this.funcio(alg.funcio(t)); } Is not going to fly because there is no way to "store" _this_ in function, use delegates (this is a context and it has to be "copied" -> use delegates). But then you'd have to change to delegate everywhere. (and there is toDelegate function in std.functional, that converts function to delegate)
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary.__funcliteral1
 cannot access frame of function
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int).opBinary
 algorisme.d:35: Error: constructor
 algorisme.Algorisme!(int,int).Algorisme.this (string nom, uint 
 versio,
 int function(int) funcio) is not callable using argument types
 (string,int,_error_ function(int t) nothrow  system)
 algorisme.d:35: Error: cannot implicitly convert expression
 (__funcliteral1) of type _error_ function(int t) nothrow 
  system to int
 function(int)
 algorisme.d:52: Error: template instance
 algorisme.Algorisme!(int,int).Algorisme.opBinary!("*",int) 
 error
 instantiating



 I update the gist: https://gist.github.com/2429005
Apr 21 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, April 20, 2012 15:37:57 Xan wrote:
 Thanks, Jonathan. I suppose with 'if' (dynamic), it generates
 Exception if we call with other operator than '*', isn't?
Exception? No. You get a compilation error. You should read http://dlang.org/template.html#Constraint http://dlang.org/version.html#staticif Template constraints are used to indicate what will and won't compile with a template and can be used to overload templates. For instance, with auto opBinary(string op)(MyType mt) if(op == "+" || op == "-") {...} auto opBinary(string op)(MyType mt) if(op == "*" || op == "/") {...} the first function would work with + and -, whereas the second would work with * and /, and those are the only overloads for opBinary, and you tried to compile with another binary operator, you'd get a compilation error. It's quite typical to do stuff like auto MyType(string op)(MyType mt) if(op == "+" || op == "-' || op == "*" || op == "/") { return MyType(mixin("this.value " ~ op ~ " mt.value")); } You use static if when you want a section of code to be different based on some condition. So, you could do something like auto MyType(string op)(MyType mt) if(op == "+" || op == "-' || op == "*" || op == "/") { static if(op == "/") enforce(mt.value != 0, "Error: Division by zero!"); return MyType(mixin("this.value " ~ op ~ " mt.value")); } The problem with your example Algorisme!(U,V) opBinary(string op)(Algorisme!(U,V) alg) { static if (op=="*") return new Algorisme!(U,V)("composició", this.versio+alg.versio, this.funcio); } what happens when you try and compile with an operator other than *. When it's *, you end up with a function looking like Algorisme!(U,V) opBinary(string op)(Algorisme!(U,V) alg) { return new Algorisme!(U,V)("composició", this.versio+alg.versio, this.funcio); } but when it's anything else, you get a function looking like Algorisme!(U,V) opBinary(string op)(Algorisme!(U,V) alg) { } It has no return value and won't compile, giving you an error about returning void or something similar. If at all possible, you should always have a template constraint on a template which restricts it to arguments which will work with that template. You can then further specialize sections of it using static if, but if you use static if only, then you get nasty compilation errors when you misuse the template. Also, you can't overload templates based on static if, so if you need to have mulitple overloads of a template, you _need_ to use template constraints. - Jonathan M Davis
Apr 20 2012