operator overloading

J

josh

Hi,

I don't find any document that say if the operator overloading will be
a new feature of the Java 7

Do anyone know if it there will be?

Thanks
 
L

Leonard Milcin

josh said:
Hi,

I don't find any document that say if the operator overloading will be
a new feature of the Java 7

Do anyone know if it there will be?

I don't think so. Why would you want it? Switch to C++ if you need it badly.

I hope they'll do such thing. I wish they forget about closures bit its
already a bit too late.

Regards,
Leonard
 
L

Leonard Milcin

josh said:
Hi,

I don't find any document that say if the operator overloading will be
a new feature of the Java 7

Do anyone know if it there will be?

I don't think so. Why would you want it? Switch to C++ if you need it badly.

I hope they'll never do such thing. I wish they forget about closures
bit its
already a bit too late.

Regards,
Leonard
 
J

josh

Simplicity is the ultimate sophistication.
                                  -- Leonardo da Vinci


for the above quaotation :)

It's better to write:

Time t = new Time(2);
Time t2 = new Time(4);

Time t3 = t + t2;

rather than

Time t3 = Time.add(t, t2);
 
L

Leonard Milcin

josh said:
for the above quaotation :)

It's better to write:

Time t = new Time(2);
Time t2 = new Time(4);

Time t3 = t + t2;

rather than

Time t3 = Time.add(t, t2);

Well. It really depends what do you mean by ,,better''. I consider the
lack of operator overloading an advantage because it helps to understand
the code. Also, operator overloading is frequently abused making it very
difficult to analyze the code.

It is not only about reading the code. Think of all the great tools like
IntelliJ IDEA. They exist only because Java is quite easy to analyze
statically. While operator overloading would not prevent static analysis
it would certainly make it harder. Think that it will make cost of
creating good tools higher which means less tools for higher price.

Think of all the people that have to learn Java. With time, Java
Language Specification gets longer and longer as it incorporates more
and more features. While it may be easy for you to learn new features
for the new release, people starting with Java have to learn it all
which gets increasingly harder. In mine opninion things like closures
only pollute language while not helping anything that couldn't be
addressed without them.

Then think that when anything is included into Java we're stuck with it
FOR LIFE or until we change the language for something different.

Isn't it much for changing


Time t3 = Time.add(t, t2);

to

Time t3 = t + t2;

?

Best regards,
Leonard
 
T

Tom Anderson

Well. It really depends what do you mean by ,,better''. I consider the
lack of operator overloading an advantage because it helps to understand
the code. Also, operator overloading is frequently abused making it very
difficult to analyze the code.

No. This is simply FUD and nothing more.

Operator overloading was abused widely in C++; it was abused by the guys
who designed the standard library (the great Bjarne himself?), and that
set an example that led to it being abused by other programmers. Smalltalk
has operator overloading, and it isn't abused there. Python has operator
overloading, and it isn't abused there either. I can show you any number
of languages with operator overloading where it isn't abused. There is
nothing inherent about operator overloading that leads to abuse - the
problem in C++ is a cultural one, not technical.

Java's designers made the argument you made, and many even have believed
it, but all this means is that they're traumatised C++ survivors, not that
they're right.
It is not only about reading the code. Think of all the great tools like
IntelliJ IDEA. They exist only because Java is quite easy to analyze
statically. While operator overloading would not prevent static analysis
it would certainly make it harder.

No, it would make absolutely no difference whatsoever. The syntax already
includes operators, and dealing with operators as method calls just
means treating "+" as a method name.
Think that it will make cost of creating good tools higher which means
less tools for higher price.

Think of all the people that have to learn Java. With time, Java
Language Specification gets longer and longer as it incorporates more
and more features. While it may be easy for you to learn new features
for the new release, people starting with Java have to learn it all
which gets increasingly harder.

They already learn how to use operators on primitives (and Strings), so
that wouldn't be an additional burden for using them on objects.

I doubt very much that learning to write overloaded operators would be
very difficult - in other languages, they're just like normal method
declarations, but with a magic word, so we might see:

// C++ style
public Point operator +(Point p) {
return new Point((x + p.x), (y + p.y)) ;
}

// python style
public Point __add__(Point p) {
return new Point((x + p.x), (y + p.y)) ;
}

// Smalltalk style
public Point +(Point p) {
return new Point((x + p.x), (y + p.y)) ;
}

// let's use the hash sign for something!
public Point #add(Point p) {
return new Point((x + p.x), (y + p.y)) ;
}

// maybe something with annotations
@operator(symbol="+")
public Point add(Point p) {
return new Point((x + p.x), (y + p.y)) ;
}

In any case, if you think it is too hard, just leave it for an advanced
chapter. There's no huge need to teach people to write overloaded
operators right at the start. In fact, it might be better not to!
In mine opninion things like closures only pollute language while not
helping anything that couldn't be addressed without them.

Oh, surely you're kidding me? I take it this means you've never used a
language with closures? One of the best things about python, one of the
things that meant i never looked back when i switched to it from java, was
the existence of lambda expressions, functions as first-class objects, and
higher-order functions.
Then think that when anything is included into Java we're stuck with it
FOR LIFE or until we change the language for something different.

Yes. And both closures and operator overloading are features which are
well-enough understood in other languages that there is essentially no
risk in adopting them in java.
Isn't it much for changing

Time t3 = Time.add(t, t2);

to

Time t3 = t + t2;

?

For your next exercise, try some matrix algebra with and without operator
overloading.

tom
 
S

Stefan Ram

From a person who is not a native speaker of English:
Is this pronounced »T plus T two«, »T add T two«,
or in yet another way?

If the previous sum was not pronounced using »add«,
why is »add« used here instead of the pronounced word?
 
S

Stefan Ram

Supersedes: <[email protected]>


From a person who is not a native speaker of English:
Is this pronounced »T plus T two«, »T add T two«,
or in yet another way?

If the previous sum was not pronounced using »add«,
why is »add« often used in expressions like

t.add( t2 )

instead of the word pronounced for the operator above?
 
O

Owen Jacobson

No. This is simply FUD and nothing more.

Operator overloading was abused widely in C++; it was abused by the guys
who designed the standard library (the great Bjarne himself?), and that
set an example that led to it being abused by other programmers. Smalltalk
has operator overloading, and it isn't abused there. Python has operator
overloading, and it isn't abused there either. I can show you any number
of languages with operator overloading where it isn't abused. There is
nothing inherent about operator overloading that leads to abuse - the
problem in C++ is a cultural one, not technical.

Java's designers made the argument you made, and many even have believed
it, but all this means is that they're traumatised C++ survivors, not that
they're right.


No, it would make absolutely no difference whatsoever. The syntax already
includes operators, and dealing with operators as method calls just
means treating "+" as a method name.



They already learn how to use operators on primitives (and Strings), so
that wouldn't be an additional burden for using them on objects.

I doubt very much that learning to write overloaded operators would be
very difficult - in other languages, they're just like normal method
declarations, but with a magic word, so we might see:

// C++ style
public Point operator +(Point p) {
        return new Point((x + p.x), (y + p.y)) ;

}

// python style
public Point __add__(Point p) {
        return new Point((x + p.x), (y + p.y)) ;

}

// Smalltalk style
public Point +(Point p) {
        return new Point((x + p.x), (y + p.y)) ;

}

// let's use the hash sign for something!
public Point #add(Point p) {
        return new Point((x + p.x), (y + p.y)) ;

}

// maybe something with annotations
@operator(symbol="+")
public Point add(Point p) {
        return new Point((x + p.x), (y + p.y)) ;

}

In any case, if you think it is too hard, just leave it for an advanced
chapter. There's no huge need to teach people to write overloaded
operators right at the start. In fact, it might be better not to!


Oh, surely you're kidding me? I take it this means you've never used a
language with closures? One of the best things about python, one of the
things that meant i never looked back when i switched to it from java, was
the existence of lambda expressions, functions as first-class objects, and
higher-order functions.


Yes. And both closures and operator overloading are features which are
well-enough understood in other languages that there is essentially no
risk in adopting them in java.




For your next exercise, try some matrix algebra with and without operator
overloading.

It might be instructive to think about how foreach was made
extensible: there is an interface which, if implemented properly, can
make an arbitrary class usable in a for(each) loop. The same could be
done to map syntax to operations:

public interface java.lang.Indexed<T> {
public T get (int i);
}

would, hypothetically, be enough to allow arbitrary classes to be used
on the left of array expressions like:

Indexed<String> notAnArray = new MyClass ("foo", "bar");
assert notAnArray[0] == "foo"; // notAnArray.get (0)
assert notAnArray[1] == "bar"; // notAnArray.get (1)

However, it's not clear whether the core operators (+, *, /, binary -,
unary -, ++, --) should be exposed as a single interface (Arithmetic),
several interfaces (Addable, Multipliable, Dividable/Rational, etc).

There are also some weird edge cases (like time) where the operands
and result of operations are not the same type. Consider:

public class Instant { ... defines a precise moment in time ... }
public class Interval { ... defines the amount of time that passes
between two Instants ... }

I'd normally want

Instant a = ..., b = ...;
Interval duration = a - b;

and not

Instant difference = a - b;

Similarly, I'd want

Instant a = ...;
Interval b = ...;
Instant future = a + b; // or b + a

Some points to consider...
-o
 
R

RedGrittyBrick

Stefan said:
From a person who is not a native speaker of English:
Is this pronounced »T plus T two«, »T add T two«,
or in yet another way?

At least one native speaker of English would always pronounce it as "tee
plus tee two".
If the previous sum was not pronounced using »add«,
why is »add« used here instead of the pronounced word?

I can't speak for the OP but I would do it that way because it more
closely follows English grammar, in terms of word order at least.

Consider:
"Add salt to water"
"Add salt and pepper"
Although, of course you can always change the word order by expressing
the idea in a more complex and awkward fashion.

If I wanted to be consistent with the normal pronunciation of "+", I
would write something like:
Time t3 = t.plus(t2);
But that might not be a good example of Java style - I'm not sure
whether Java programmers would expect the value of t to be altered.



BTW It doesn't really make sense (to me) to add two times together. What
is 1 p.m today plus half past three yesterday?

Another language has data types of DateTime and Interval and allows
Intervals to be added to DateTimes.
 
S

Stefan Ram

RedGrittyBrick said:
BTW It doesn't really make sense (to me) to add two times together. What
is 1 p.m today plus half past three yesterday?

It should be something like:

final Instant instant = new Instant( "2008-05-08T18:12:41+02:00" );
final Interval interval = new Intervall( "02:00:00" );
final Instant instant1 = instant.plus( interval );
 
A

Andrea Francia

josh said:
for the above quaotation :)

It's better to write:

Time t = new Time(2);
Time t2 = new Time(4);

Time t3 = t + t2;

rather than

Time t3 = Time.add(t, t2);

I don't find correct adding two Times.
I think is more correct adding a Time to a TimeSpan.
With or without operators.
 
T

Tom Anderson

Supersedes: <[email protected]>



From a person who is not a native speaker of English:
Is this pronounced »T plus T two«, »T add T two«,
or in yet another way?

Always "plus" for me.
If the previous sum was not pronounced using »add«,
why is »add« often used in expressions like

t.add( t2 )

instead of the word pronounced for the operator above?

It's traditional to use verbs for method names. 'Add' is a verb; 'plus'
isn't. I would say that in this case, 'plus' might well be a better name,
because this:

a.plus(b.times(c)).dividedBy(d)

Reads better than this:

a.add(b.multiply(c)).divide(d)

tom
 
T

Tom Anderson

It might be instructive to think about how foreach was made extensible:
there is an interface which, if implemented properly, can make an
arbitrary class usable in a for(each) loop. The same could be done to
map syntax to operations:

public interface java.lang.Indexed<T> {
public T get (int i);
}

would, hypothetically, be enough to allow arbitrary classes to be used
on the left of array expressions like:

Indexed<String> notAnArray = new MyClass ("foo", "bar");
assert notAnArray[0] == "foo"; // notAnArray.get (0)
assert notAnArray[1] == "bar"; // notAnArray.get (1)

That's a bloody good idea. Well, it might be ...
However, it's not clear whether the core operators (+, *, /, binary -,
unary -, ++, --) should be exposed as a single interface (Arithmetic),
several interfaces (Addable, Multipliable, Dividable/Rational, etc).

Both. The joy of multiple inheritance of interfaces!

You need to be able to have things which you can, say, add and multiply,
but not divide - anything a mathematician would call a ring, like a
three-by-three matrix. Forcing these to have divide methods that just
threw an exception would be pretty poor.

You could even model a little hierarchy on discrete mathematics: Addable,
Multipliable, Ring extends Addable, Multipliable, Dividable, Field extends
Ring, Dividable, etc.
There are also some weird edge cases (like time) where the operands
and result of operations are not the same type.

There are also some distinctly non-weird non-edge cases where this is
true, and where the operands are not the same type, and where you want to
be able to overload the operators according to operand type. An obvious
example would be a Vector3D; i want to be able to add two vectors to make
a third, but also to multiply a vector by a double to get a vector, and a
vector by a vector to get their dot product, which is a double. If i also
have Matrix3D (being a 3 x 3 matrix, to represent linear transformations),
i want to be able to add matrices, to multiply two to make a third
(although i have no idea what the geometric significance of that is!), to
multiply a matrix by a vector to make a transformed vector, etc.

I can't see how you'd do this with interfaces unless they were defined as
taking Object and returning Object, which would be a bit lame. Like how
equals and compareTo work.

tom
 
J

Joshua Cranmer

josh said:
Hi,

I don't find any document that say if the operator overloading will be
a new feature of the Java 7

Do anyone know if it there will be?

There are proposals, in great contention as always, but the answer right
now is "probably not." I have somewhere some ancient links on the matter
(~1 year old); if you want, I could try to dredge them up.
 
J

Joshua Cranmer

Tom said:
I doubt very much that learning to write overloaded operators would be
very difficult - in other languages, they're just like normal method
declarations, but with a magic word, so we might see:

[ snip styles ]

This is the kind of style I've envisioned myself:

public class Point implements Addable<Point, Point> {
// etc.
public Point add(Point p) {
return new Point((x + p.x), (y + p.y));
}
}

with Addable of course being:
public interface Addable<A,R> {
public R add(A addend);
}

Oh, surely you're kidding me? I take it this means you've never used a
language with closures? One of the best things about python, one of the
things that meant i never looked back when i switched to it from java,
was the existence of lambda expressions, functions as first-class
objects, and higher-order functions.

Bloch (I think it was him) had a presentation a while back on why
closures and Java don't mix; I myself was irrevocably convinced that
they were a poor idea when I saw that "return" and "return;" would mean
different things under the BGGA proposal (and then some people arguing
that the differences were "obvious"). Java's a great object-oriented
language; don't try to force it to become a functional one as well.
Yes. And both closures and operator overloading are features which are
well-enough understood in other languages that there is essentially no
risk in adopting them in java.

As Bloch points out in his presentation, there is a high risk. It mixes
with generics and autoboxing in such a way that some things that seem
like they should work just plain don't and can't. There is also the
great chance that you end up with a situation not unlike what happened
with generics: it goes only halfway and you end up with some aggravating
problems.
 
J

Joshua Cranmer

Leonard said:
I hope they'll do such thing. I wish they forget about closures bit its
already a bit too late.

My personal take on the matter is that closures will not make it into
Java 7. I took a survey of this newsgroup about some proposed Java 7
features, and closures was mostly met with indifference. If I
extrapolate, I would say that most programmers won't care, and a little
more will lobby against it than will lobby for it. Certainly, a little
bit of anti-BGGA press would go a long way to killing it, if you really
want to do so.
 
S

Stefan Ram

Joshua Cranmer said:
My personal take on the matter is that closures will not make it into
Java 7. I took a survey of this newsgroup about some proposed Java 7
features, and closures was mostly met with indifference.

But, when one goes to night clubs asking young women:

http://bp0.blogger.com/_lfuDZjhLbAM/RsjUjleoCfI/AAAAAAAAACU/IXyIrODh2nM/s1600/P8180010.jpg
http://bp3.blogger.com/_lfuDZjhLbAM/RsjU8VeoCgI/AAAAAAAAACc/ZRh0eqEGef0/s1600/P8180004.JPG
http://bp3.blogger.com/_lfuDZjhLbAM/RsjVTVeoChI/AAAAAAAAACk/7OAqSfZZogg/s1600/P8180009.jpg
http://bp3.blogger.com/_lfuDZjhLbAM/RsjVuVeoCiI/AAAAAAAAACs/lTnXY4GyHf4/s1600-h/P8180012.jpg

(I experience server problems with some of these pictures
right now, but some were visible.)
 
R

Roedy Green

I don't find any document that say if the operator overloading will be
a new feature of the Java 7

Usually when the matter comes up, there is a quite a squawk of "over
my dead bodies".

My view is user defined operators with new names/symbols would be
acceptable, but overloading ordinary >> to do I/O is as C++ is an
abomination. Unicode has lots of symbols you could use as
user-defined operators.
 
P

Patricia Shanahan

Roedy said:
Usually when the matter comes up, there is a quite a squawk of "over
my dead bodies".

My view is user defined operators with new names/symbols would be
acceptable, but overloading ordinary >> to do I/O is as C++ is an
abomination. Unicode has lots of symbols you could use as
user-defined operators.

How about the use of "+" for e.g. complex number addition? Can you
suggest a better choice of symbol?

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top