July 26, 2002
Forgive me for joining a conversation I have not followed but I saw some comments about operators.

Has anyone looked into blitz++?  This is a cool C++ library with special expressiveness.

Given the chance to define a whole new language better than C++ I should think D could do at least as well.  Take a peek at it!

Sorry if this comment is not exactly on-topic.

M.


July 26, 2002
For whatever my vote is worth -- as a long time user of mathematics systems like Mathematica -- I would like commutivity to be user-definable.

Even though Mathematica is a fantastic symbolic solver, it is awful for noncommutative work, precisely because of built in assumptions about commutivity.  I would rather the compiler let me tell it what I want for any given operator.

M.


July 30, 2002
"Walter" <walter@digitalmars.com> wrote in message news:ahnj39$133m$1@digitaldaemon.com...
>
> "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ags8db$2eot$1@digitaldaemon.com...
> > The best thing to me still seems to be giving Object
> > a few extra methods, such as add(), sub(), mul()
> > and div() and map the operators to calls to these
> > functions. This is already done for == I believe?
>
> This is a pretty good idea.

Gee, Walter, so you are not completely against operator overloading, after
all.
But what would be the parameters of those methods in Object? I hope not:
void add(Object a, Object b)
Because "2" is not an Object. So you already need some kind of template
here.

> My only objection is that I never liked the C++
> approach of being able to override the global operator+ too, producing the
> confusing combination of:
>
> struct A
> {
>     int operator + (X);
> };
> int operator+ (A, X);
>
> Note the inherent asymmetry of this, especially if A::operator+ turns out
to
> be virtual. The STL has some complicated examples of how to get into
trouble
> with this, especially when you throw templates into the mix.
>
> A better approach would be to simply disallow overloading the global operators, and say "no" to overloading the syntax:
>
>     1 + A;

Saying "no" seems too restrictive. And counter-intuitive, isn't it? But as told on other threads, with some syntax sugar, you don't need global operator for this.

> An alternative is for the compiler to rewrite (1+A) as (A+1) and then look
> for an operator+ overload, but that can lead to trouble for (A+B) where
both
> A and B overload operator+.

How would you rewrite 1-A ?

Sandor


July 30, 2002
On Tue, 30 Jul 2002 11:00:57 +0200 "Sandor Hojtsy" <hojtsy@index.hu> wrote:

> Gee, Walter, so you are not completely against operator overloading, after
> all.
> But what would be the parameters of those methods in Object? I hope not:
> void add(Object a, Object b)
> Because "2" is not an Object. So you already need some kind of template
> here.

I guess it'd work like this:

	class Vector
	{
		...
		Vector mul(Vector v) { ... }		// Vector + Vector
		Vector mul(int n) { ... }		// Vector + int
	}

But then, what about int + Vector?
I still think static methods with special syntax would be better:

	class Vector
	{
		...
		static operator* (Vector a, Vector b) { ... }
		static operator* (Vector a, int b) { ... }
	}

Short and clear.

> How would you rewrite 1-A ?

-(A-1), assuming that we can overload unary operators as well.
But it might not always work.
July 30, 2002
"Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374676619595486@news.digitalmars.com...

>> How would you rewrite 1-A ?

>-(A-1), assuming that we can overload unary operators as well.
>But it might not always work.

Only if unary minus is actually overloaded. Or would you require to overload
it for all cases where the binary minus is overloaded? And also restrict
binary minus operations to those which satisfy -(A-1) == 1-A ??
For example: You are creating a class to implement a "set" of integers.
Set a = new Set, b = new Set;
a ~= 1; a ~= 3; a ~= 6;
b ~= 3; b ~= 4;
Set c = a - b;
As defined by the well known set operations, "c" will contain the elements
which are present in "a", but not in "b".
Now -(a-b) != b-a. Yes, I know you don't need to reorder here, but the
example shows that you should not altogether disallow binary minus
where -(a-b) != b-a. So you cannot reorder in all cases. How should the
compiler now when is it OK to reorder? It brings in more problems, than the
few it solves.
Conclusion: This reorder idea is Baaad.

Yours,
Sandor


July 30, 2002
On Tue, 30 Jul 2002 16:52:25 +0200 "Sandor Hojtsy" <hojtsy@index.hu> wrote:

> Conclusion: This reorder idea is Baaad.

Agreed. I just wanted to show how it _could_ be done. But I definitely don't like it.
July 30, 2002
"Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374676619595486@news.digitalmars.com...

>I guess it'd work like this:
>
>class Vector
>{
>...
>Vector mul(Vector v) { ... } // Vector + Vector
>Vector mul(int n) { ... } // Vector + int
>}
>
>But then, what about int + Vector?

   As I said, a simple and complete solution would be:

class Vector
{
...
Vector mul(Vector v) { ... } // Vector + Vector
Vector mul(int n) { ... } // Vector + int
Vector reverse_mul(int n) { ... } // int + Vector
}

   Ahem... being verbose ;-)

Salutaciones,
                         JCAB



July 31, 2002
"Pavel Minayev" <evilone@omen.ru> wrote in message
news:CFN374676619595486@news.digitalmars.com...
On Tue, 30 Jul 2002 11:00:57 +0200 "Sandor Hojtsy" <hojtsy@index.hu> wrote:
>
> I still think static methods with special syntax would be better:
>
> class Vector
> {
> ...
> static operator* (Vector a, Vector b) { ... }
> static operator* (Vector a, int b) { ... }
> }
>
> Short and clear.
>

Sounds good, you have my vote on this!
I don't know about operator* as a name though..., this would
forbid calling the function directly wouldn't it? I can imagine
some operator-overloading haters not being happy with
that...If you would call it mul you could also call it directly,
some people might prefer that...


--
Stijn
OddesE_XYZ@hotmail.com
http://OddesE.cjb.net
_________________________________________________
Remove _XYZ from my address when replying by mail




July 31, 2002
"Pavel Minayev" <evilone@omen.ru> wrote in message
news:CFN374676619595486@news.digitalmars.com...
On Tue, 30 Jul 2002 11:00:57 +0200 "Sandor Hojtsy" <hojtsy@index.hu> wrote:

> class Vector
> {
> ...
> static operator* (Vector a, Vector b) { ... }
> static operator* (Vector a, int b) { ... }
> }

> Short and clear.

I think * would be confused with pointers.

> > How would you rewrite 1-A ?

Juan offered a better solution I think because you get proper access to the object properties and it's less special-case.


July 31, 2002
anderson wrote:
> "Pavel Minayev" <evilone@omen.ru> wrote in message
> news:CFN374676619595486@news.digitalmars.com...
> On Tue, 30 Jul 2002 11:00:57 +0200 "Sandor Hojtsy" <hojtsy@index.hu> wrote:
> 
> 
>>class Vector
>>{
>>...
>>static operator* (Vector a, Vector b) { ... }
>>static operator* (Vector a, int b) { ... }
>>}
> 
> 
>>Short and clear.
> 
> 
> I think * would be confused with pointers.
> 
> 
>>>How would you rewrite 1-A ?
>>
> 
> Juan offered a better solution I think because you get proper access to the
> object properties and it's less special-case.
> 
> 

You would have to have different 'keywords' for each symbol, i.e,
for vectors you might want to overload * for cross product and
constant multiples, but have . for dot product also, which one
would mul be? I suppose you could have a dot keyword also, but I
like the idea of being able to explicitly specify which symbol to
use.

Maybe something like
class Vector
{
     static operator(*) cross(VectorA, VectorB);
     static operator(*) mul(VectorA, int);
     static operator(.) dot(VectorA, VectorB);
     .... etc.
}

This way, those who don't like operator overloading could
still call the functions by name, i.e. cross(), dot() etc.
and you could specify which symbol to use also.
Just an idea,
 -Jon