Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Little rant  (Read 2887 times)
0 Members and 1 Guest are viewing this topic.
Offline Java Cool Dude

Senior Member




Java forever


« Posted 2004-02-18 10:27:17 »

Why can't we have operator overload in Java?
It sickens me to no end to see other languages using that for their game programming such as adding vectors and substracting them with simple -/+ instead of the crappy .add/.sub that we're accustomed to in Java.
Everytime I talk to one of those die-hard c++ programmers about what I can do with Java gaming wise they burst laughing at how we can't use what they think of as mandaotry in game syntax and coding.
Geesh Sun must have one of them ultra-good reasons to not allow us to play around with em operators.. Roll Eyes
/end rant
Offline Mark Thornton

Senior Member





« Reply #1 - Posted 2004-02-18 10:49:52 »

The widespread abuses of operator overloading in C++ have made this a religious issue. Those who oppose operator overloading do so with a fervour unmatched by any ayatollah! Most of this could be defused if we could find a way to restrict such overloading to arithmetic types. It doesn't help that '+' has already been overloaded for String values.
The best I can suggest is that operator overloading be restricted to argument types which are either primitive or immutable classes. This would remove some of the more flagrant abuses.
Offline princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2004-02-18 11:11:19 »

Why can't we not have operator overloading but instead have operator style method placement? eg.

1  
2  
Vector3f a, b;
float cos = a dot b;


where dot is defined as something like
1  
2  
3  
4  
5  
class Vector3f {
public static operator float dot(Vector3f left, Vector3f right) {
//etc.
}
}


in other words, legal Java identifiers, and following a simple semantic that the method must be static to the class with the same class of operand as the left parameter? Would this work nicely?

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2004-02-18 11:14:42 »

What about

1  
2  
3  
Vector3f a,b,dot;

float cos = a dot b;


How does this work?

Kev

PS. Operator overloading is another case of a language taking the tools away for the fear of people misusing them.

Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #4 - Posted 2004-02-18 11:24:36 »

Nooo!!!! Operator overloading is so far from elegance I think everyone should move to my model:


MathFactory.3DMathMonostate.VecMath.Vectors.TranslationSingleton.translateVector4f(planeNormal.getNormal(), planenormal.getD)

A lot easier than using:
Vector4f transf_position = triangle.position * view_transformation;

I don't really care for PrinceC's model, however operators are nice. Besides it wouldn't be that hard to abuse them and only complete idiots would use * as dot or crossproduct. Besides overloading new and delete would be obsolete in java and thus this would remove 90% of the abuses.
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #5 - Posted 2004-02-18 11:31:49 »

Quote
What about

1  
2  
3  
Vector3f a,b,dot;

float cos = a dot b;


How does this work?


That fails to compile, as it isn't valid syntax.  As far as I can think, there is no place in a Java program that two variable identifiers are placed side by side with only whitespace between them.

Cas, you don't even need to make that static - it could easily be a member method executed on the first argument.

Quote
PS. Operator overloading is another case of a language taking the tools away for the fear of people misusing them.


Almost.  The language took the tools away because of the number of people misusing them. Grin

Hellomynameis Charlie Dobbie.
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2004-02-18 11:36:42 »

Quote

Almost.  The language took the tools away because of the number of people misusing them.


Hey, yeah, fair point.

Quote

That fails to compile, as it isn't valid syntax.  As far as I can think, there is no place in a Java program that two variable identifiers are placed side by side with only whitespace between them.


True, is there a place where any two symbols (variables or method names) are placed next together currently?

Kev

Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #7 - Posted 2004-02-18 11:48:24 »

Only fools and newbies used operator overloading in C++ w/o his back to the wall and a knife at his throat! You'll NEVER find the bugs and the code is harder to read!
The rules used to find out which operator actually has to be used have been VERY complex and impossible to have in mind in daily work!

1  
2  
3  
4  
5  
Triangle a = new Triangle();
Square b = new Square();
Shape c = a + b;   // what happens? Lookup all overloads for '+' and all possible type conversion/casts

Shape d = Shape.add( a, b ); // any doubt whats going on?


What if the there is a +(Shape,Shape), another +(Shape,Square) and a +(Shape,Square)? Some delivering Circles, other Squares, other float?

I personnally spent TOO much time tracking down bugs where someone just wanted to write '+' and thought it was cool!

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2004-02-18 11:49:47 »

Operator overloading is great when I'm using std::string and its overloaded operators let me chuck them at various functions and have it automagically do casts and conversions. Grin

Operator overloading is a real pain when I use an assignment operator and I'm never quite sure what I've actually received... Shocked

Take your pick, I can live with them or without them. But when I'm using someone elses API thats got funny overloading it can cause all sorts of confusion.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #9 - Posted 2004-02-18 12:02:46 »

Quote
True, is there a place where any two symbols (variables or method names) are placed next together currently?


Well, you can mix plain-text operators with types and variables, and keywords with types and method or variable names:

1  
2  
3  
if(object instanceof String) {...}
public static final void blah(int arg){...}
protected String foo ;


But that's all the plain-text collisions I can think of!  Anyone else? Huh

Hellomynameis Charlie Dobbie.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #10 - Posted 2004-02-18 12:09:58 »

Quote

Well, you can mix plain-text operators with types and variables, and keywords with types and method or variable names:


Sure, not symbols tho. Wasn't really trying to argue the case, just couldn't think of one right off  Grin

Kev

Offline Mark Thornton

Senior Member





« Reply #11 - Posted 2004-02-18 13:45:04 »

For vector 'multiplication' it would be far better to be able to define new operators (for dot and cross products) rather than overload existing operators. However as the obvious candidates don't appear on most people keyboards working with these would be awkward.
There is nothing stopping someone from writing an IDE which automatically renders some methods in infix form with an appropriate symbol (define the symbol to be used in the JavaDoc comment for the method). If you include the precedence information you could also avoid adding unnecessary parentheses. The smart IDE might then allow you to type '*' to multiply two vectors and then offer you a choice of the two alternatives.
Offline AndersDahlberg

Junior Member





« Reply #12 - Posted 2004-02-18 14:13:44 »

1  
2  
3  
4  
5  
...
import javax.math.Vector; // static import

...
Vector3f result = cross(vec1, vec2);


Or something similiar - doesn't seem too complex, or?
Offline princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2004-02-18 17:06:17 »

It still doesn't "read" like how you would naturally write it.

So what is so wrong with being able to write methods in an alternative syntax? What's wrong with a dot b instead of a.dot(b)? Or lengthof a instead of a.lengthOf()? It'd make the language beautiful to the human reader. You could design your APIs so that they could be read.

Cas Smiley

Offline morbo

Senior Newbie





« Reply #14 - Posted 2004-02-18 17:26:19 »

I love circles...

You're describing Smalltalk (the original OO language), Cas. The language grammer describes statements as noun-verb pairs. So if you have a string 'name', you can do things like:

name lengthOf - calls the 'lengthOf' method on name
name + name2 - calls the '+' method on name sending name2 as a parameter

Though smalltalk doesn't call them methods: they're events.

It would be interesting to see if one could create a Smalltalk-like language that compiles to bytecodes. Hmm...
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #15 - Posted 2004-02-18 20:54:18 »

Quote
It would be interesting to see if one could create a Smalltalk-like language that compiles to bytecodes. Hmm...


http://grunge.cs.tu-berlin.de/~tolk/vmlanguages.html#smalltalk  Grin

Hellomynameis Charlie Dobbie.
Offline sma

Junior Member





« Reply #16 - Posted 2004-02-21 09:18:17 »

<Smalltalk rant>
Unfortunately, all three implementations listed have heavy drawbacks and don't feel like Smalltalk at all (at least IMHO). And there's not as freely available as I'd wish.

The last time I checked, SmalltalkJVM requires VisualAge Smalltalk. You use that fine Smalltalk development environment and once ready, you can transform your code. While debugging in Smalltalk is usually a pleasure, you cannot debug cross-language. They want my name for a windows-only download, so I refrain.

Talk2 feels more like Smalltalk but the demo doesn't really work and the website doesn't provide any deeper information and again, I have to fill some some form to download, so I refrain.

Bistro is freely available but a bastard of Java and Smalltalk. To interface Java, a lot of annotations were introduced, it feels somehow alien to an old Smalltalker like me.

And all projects seem to have stoped development in 2002 or earlier (Talk2 talks about upgrading to Java 1.2 sometime?!)

While you can certainly create a Smalltalk-like language that compiles down to a JVM, it is really difficult to create efficient code. Smalltalk's more flexible features can't be mapped onto the JVM directly.  Rhino and Jython need to solve similar problems.

Two of the main performance blockers are, that you'd have to root each method call through some invoker method (Groovy does this and on a modern VM, the performance penalty doesn't seem that high any more, we're talking about 5-10x slower method dispatch) and even a single "+" on two numbers would have to check for overflows and must continue to work with BigIntegers seamlessly.  You probably wouldn't be able to reimplement Smalltalk-style exceptions at all (Java's try-blocks can't be restarted) and metaprogramming stuff like #allInstances or #become: is probably also out of reach.

Having said that, a decent Smalltalk that compiled to bytecode could be SharpSmalltalk written by John Brant and Don Robertson (I think). These are the two guy who invented the Smalltalk RefactoringBrowser, made the idea of refactoring popular which eventually inspired Fowler to write books about this topic and gave us Refactorings in IDEs like Eclipse or IDEA.  SharpSmalltalk is written for .NET but that platform has the same limitations as the JVM so it should be possible to use their technology to create something similar for the JVM.

However, if all what you want is just a fancy "object message" syntax, check out Objective-C which was Brad Cox answer to Smalltalk, using C to add object oriented programming.  It's an interesting language and you might wonder why there's no Objective-Java for advanced OO-Programming in Java Smiley

And as already mentioned, the Groovy languages tries to combine the best ideas of Java (assuming there are any), Python, Ruby (and therefore Smalltalk) and Lisp to create a decent scripting language especially for the JVM.  Interesting goal, but they're not yet there, the current version has still a lot of rough edges.

Last but not least, I'd like to have operator overloading although I don't want operators?  Strange? Well, operators are a legancy of primitives datatypes and I'd wish both would have never been part of the language.  Something like "a + b" is just syntactic sugar for "+(a, b)" and that way, yes, I'd like to have this kind of binary functions or methods in a language.  I'd also like [] and []= as method, to ease indexed access to collections.  And I'd never have used + for strings, because that creates unexpected results if you use "a+b="+3+4.  

Whatever, that's all just a dream, I don't think that Java will change and it's not the language that I like but the development environment plus the libraries.  Hopefully, languages like Groovy or Scala (another very interesting JVM-based functional language) will evolve and will eventually get tool support like Java itself.
</rant>

.: Truth Until Paradox!
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #17 - Posted 2004-02-23 07:36:40 »

Quote
...I have to fill some some form to download, so I refrain.


The mailinator is thy saviour.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!