Jump to page: 1 2
Thread overview
Operator overloading
Apr 19, 2012
Xan
Apr 19, 2012
Dmitry Olshansky
Apr 19, 2012
Xan
Apr 19, 2012
Jonathan M Davis
Apr 20, 2012
Xan
Apr 20, 2012
Xan
Apr 20, 2012
Xan
Apr 20, 2012
Dmitry Olshansky
Apr 20, 2012
Xan
Apr 20, 2012
Dmitry Olshansky
Apr 20, 2012
Xan
Apr 20, 2012
Xan
Apr 20, 2012
Dmitry Olshansky
Apr 21, 2012
Xan
Apr 20, 2012
Jonathan M Davis
April 19, 2012
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.
April 19, 2012
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
April 19, 2012
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.


April 19, 2012
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
April 20, 2012
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.
April 20, 2012
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.

April 20, 2012
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)
April 20, 2012
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
April 20, 2012
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.
April 20, 2012
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
« First   ‹ Prev
1 2