www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - operators

reply james keogh <james_member pathlink.com> writes:
one feature i have often wish for in a language is the ability to define ne
operators - to leep the code readable new operators would have to begin and or
end with a reserved symbol i think.

some thing like £operatorname for post operatorname£ for pre and £operatorname£
for binary operator.

on the other hand perhaps i wont need this as much with d as there is a seperate
concat operator to arithmetic which is where the feature is usually wanted in
other languages

to workout my email address
firstname.lastname astrium.eads.net 
Aug 16 2004
next sibling parent reply "Garett Bass" <gtbass studiotekne.com> writes:
When I first learned of the operator overload syntax in C++ I was very
excited, I immediately planned to write the following:


class Vector
{
    int    operator·(Vector &other); // dot product
    Vector operator×(Vector &other); // cross product
}

You can imagine my disappointment when I discovered you can only overload
the existing scalar math operators that are already part of the language.  I
thought the above would make some nice syntactic sugar for linear algebra
code.

Oh well :P


"james keogh" <james_member pathlink.com> wrote in message
news:cfq36a$24lb$1 digitaldaemon.com...
 one feature i have often wish for in a language is the ability to define

 operators - to leep the code readable new operators would have to begin

 end with a reserved symbol i think.

 some thing like £operatorname for post operatorname£ for pre and

 for binary operator.

 on the other hand perhaps i wont need this as much with d as there is a

 concat operator to arithmetic which is where the feature is usually wanted

 other languages

 to workout my email address
 firstname.lastname astrium.eads.net

Aug 16 2004
parent reply james keogh <james_member pathlink.com> writes:
In article <cfqn3q$2kk0$1 digitaldaemon.com>, Garett Bass says...
When I first learned of the operator overload syntax in C++ I was very
excited, I immediately planned to write the following:


class Vector
{
    int    operator·(Vector &other); // dot product
    Vector operator×(Vector &other); // cross product
}

You can imagine my disappointment when I discovered you can only overload
the existing scalar math operators that are already part of the language.  I
thought the above would make some nice syntactic sugar for linear algebra
code.

Oh well :P

matrix and vector operation would certainly be a strong candidate for this sort of thing. other less used arithmetic operators could become operators instead of functions perhaps we could persuade all those physicists still using fortran to switch if they were allowed a raise to the power of operator? to workout my email address firstname.lastname astrium.eads.net
Aug 17 2004
parent reply Nick <Nick_member pathlink.com> writes:
In article <cfshfj$tmd$1 digitaldaemon.com>, james keogh says...
matrix and vector operation would certainly be a strong candidate for this sort
of thing. 

Firstly, like someone noted, most people wouldn't have a clue how to type · or × (center dot and "x", do they even display correctly?) Secondly, it's a bad idea for the sake of readability to allow arbitrary operators with which readers have no previous experience. For example, which do you think is clearest? Tensor a, b; .. a = a &% b; a = a.tensorProd(b);
perhaps we could persuade all those physicists still using fortran to switch if
they were allowed a raise to the power of operator?

You can use ^, opXor :-) Nick
Aug 17 2004
next sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <cftr6i$1f82$1 digitaldaemon.com>, Nick says...

perhaps we could persuade all those physicists still using fortran to switch if
they were allowed a raise to the power of operator?

You can use ^, opXor :-) Nick

It's kind of already in use. When I implemented the Int class, I had to use opXor() for, well, XOR. Basic and others use operator ** to mean "raised to the power of". It would be nice if D could introduce that. Its precedence is known (higher than *) and so is its associativity (not associative, evaluate right-to-left). Its override could be called opPow(). Just a thought. Arcane Jill
Aug 17 2004
parent james keogh <james_member pathlink.com> writes:
You can use ^, opXor :-)

Nick

It's kind of already in use. When I implemented the Int class, I had to use opXor() for, well, XOR.

exactly is you want something other than the predefined operators it would be nice to beable to give it a different name - and something more meaningfull than a single char ie £pow£
Basic and others use operator ** to mean "raised to the power of". It would be
nice if D could introduce that. Its precedence is known (higher than *) and so
is its associativity (not associative, evaluate right-to-left). Its override
could be called opPow().

Just a thought.
Arcane Jill

hmm, must admit i hadnt thought about operator precedence when defining new operators. either youd need some way of stating what the precedence was for your operator or youd have to have all user defined operators at the same precedence which would then require lots of () to get expressions correct to workout my email address firstname.lastname astrium.eads.net
Aug 18 2004
prev sibling parent james keogh <james_member pathlink.com> writes:
In article <cftr6i$1f82$1 digitaldaemon.com>, Nick says...
In article <cfshfj$tmd$1 digitaldaemon.com>, james keogh says...
matrix and vector operation would certainly be a strong candidate for this sort
of thing. 

Firstly, like someone noted, most people wouldn't have a clue how to type · or × (center dot and "x", do they even display correctly?) Secondly, it's a bad idea for the sake of readability to allow arbitrary operators with which readers have no previous experience. For example, which do you think is clearest? Tensor a, b; .. a = a &% b; a = a.tensorProd(b);

i think you are missing the point the operator you would write would not be a meaniningless symbol but something like d = a £foo_op£ b £foo_op£ c (i know £ isnt a good char for the begin/end op name but it shows the intent) this would be better than d = (a.foo_op(b)).foo_op(c) to my mind anyway - you also distinguish between differt unary and binary ops easily b= pre_op£a b= a£post_op c= a £bin_op£ c to workout my email address firstname.lastname astrium.eads.net
Aug 18 2004
prev sibling next sibling parent reply teqDruid <me teqdruid.com> writes:
On Mon, 16 Aug 2004 10:44:26 +0000, james keogh wrote:

 one feature i have often wish for in a language is the ability to define ne
 operators - to leep the code readable new operators would have to begin and or
 end with a reserved symbol i think.
 
 some thing like £operatorname for post operatorname£ for pre and £operatorname£
 for binary operator.

Yeah, but half the programmers using your class wouldn't know how to type the £ character, and would have to copy-paste it out of the documentation like I just did out of your post. :)
 
 on the other hand perhaps i wont need this as much with d as there is a
seperate
 concat operator to arithmetic which is where the feature is usually wanted in
 other languages
 
 to workout my email address
 firstname.lastname astrium.eads.net

Aug 17 2004
parent james keogh <james_member pathlink.com> writes:
Yeah, but half the programmers using your class wouldn't know how to type
the £ character, and would have to copy-paste it out of the documentation
like I just did out of your post.

:)

true - we can make it the '€' character then to make it easier :-D there must be some character that isnt already a symbol and is on most keyboards hows about # or to workout my email address firstname.lastname astrium.eads.net
Aug 17 2004
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
One problem you have to consider is that the operator precedence has to=20
be solved in a parser someway. As it is now, it's solved by formulating=20
a grammar which solves the priorities implicitly. I'm afraid explicit=20
priority solving like is requiered for unforeseen operators, if they are =

allowed to have different priorities anyway, would be somewhat brittle=20
and wouldn't fit well.

-eye

james keogh schrieb:

 one feature i have often wish for in a language is the ability to defin=

 operators - to leep the code readable new operators would have to begin=

 end with a reserved symbol i think.
=20
 some thing like =A3operatorname for post operatorname=A3 for pre and =A3=

 for binary operator.
=20
 on the other hand perhaps i wont need this as much with d as there is a=

 concat operator to arithmetic which is where the feature is usually wan=

 other languages
=20
 to workout my email address
 firstname.lastname astrium.eads.net=20

Aug 17 2004