Just give us operator overloading!

I just came across this post. It looks like there’s talk to introduce special operators for getters and setters in Java 7! The following two lines might be equivalent:

a.setFoo(b.getFoo());
a->Foo = b->Foo;

See page 27 of this PDF.

It’s just so wrong to me. The problem only exists because field access is done by static type, if they did the same lookup as they do for methods, we could just use ‘.’. If you wanted to do something different in a setter or a getter, then you could explicitly write a method.

The suggestions on page 28 make more sense, e.g. String support in switch blocks, and comparison operators for enums. But why not just give us generic support? Why can’t I use any object in a switch statement? Surely it could just invoke equals()? Similarly give me the chance to define what -> means, or add comparison operator support to any class I want. Then I wouldn’t mind stupid things like ->, but this whole, some objects are more equal than others thing when it comes to operators, and stupid operator definitions, that I can’t abide! ;)

Spread the word: Technorati related  |  Technorati related  |  del.icio.us bookmark it!  |  submit Just give us operator overloading! digg.com digg it!  |  reddit reddit!

9 Responses to “Just give us operator overloading!”

  1. Shai Almog says:

    No don’t give us operator overloading, it complicates the language both for the developer and the compiler. No wonder in this day and age that C++ is STILL not portable to many platforms on which the much younger Java runs flawlessly! (without Java 5 features obviously since they are mostly redundant on a cell phone etc.).
    I agree the -> feature is stupid but so is overloading which is just a way to make code less readable, people often misuse this feature by breaking the operators semantics and then blame the resulting bugs on the language/library.

  2. Daniel Lukic says:

    Are we still in Kindergarten? :) Blocks, operator overloading, etc are neccessary features if you want to play with the adults. Whether Java should support it is a different question. I opt for a Java for idiots as it is now. Remove annotations etc. And, additionally, introduce a JavaPro for ppl who really want to get work done. Introduce blocks, op overloading, make typing optional, etc.

    What I’m saying: I understand Shai. ‘Normal’ developers need an easy language. But if you want to support advanced developers, you need to provide them with the right tools.

    Anyway, it doesn’t really matter because there are good alternatives. A JavaPro could be very cool. But all we need is good VMs. Because Ruby, Python, Lisp, etc are already - and existing - nice alteratives to a JavaPro.

    My two cents..

    dl

  3. Shai Almog says:

    I didn’t call you a child for expressing an opinion ;-)
    My dad must have missed the whole discussion about operator overloading when I had my bar-mitzva so I don’t see where they become “adult” features in a language. Especially not where most languages commonly used don’t have them…

    I don’t just say that “normal” developers don’t need operator overloading, I’m saying that no one needs it and its not an advanced feature. Its a feature with no real use cases outside of niche uses (vector calculations) in which case using a method would work just as well and be far more readable to all persons involved. I cannot “grep” (and I use the word grep to indicate using the IDE’s search functionality) a usage of “+” and if operators are overloaded it is often hard to tell what is happening in a narrow block of code.
    Unlike many language features you could add to Java, operator overloading provides no technical advantage other than hiding “the bare metal” so you don’t see that a method call is being made in order to perform the operation you requested. When dealing with large bodies of code this is hard on the most experienced programmer, I see no advantage other than the: “cool I can use the x operator for my custom object” argument.

    About ruby, python and lisp notice several things:
    1. Different languages with different audiences - scripting languages tend to target ad hoc development which is great but they are not competitors to Java.
    2. Ruby and python are running on top of the Java VM with some technical advantages on their “native” VM. This is supported by Sun and is a part of the concept of one language can’t fit everyone.
    3. Everyone is welcome to use the language of his preference but this does not make Ruby less than Java or the other way around, a feature has objective aspects and subjective aspects and language features often are very subjective.

  4. Simon says:

    Yes to generic support in switch blocks - very useful.

    No to operator overloading - if you want it use c++

  5. Miles says:

    Do I actually want operator overloading in Java? I’m still undecided about that. There are some cases where I’d like it, the overloading of ‘.’ and ‘=’ in Ruby is a much more elegant way to solve the getters and setters issue.

    The main issue I have is this partial operator overloading that Sun is doing, is that the classes are part of the standard library, not the language. It started out with String and ‘+’, then we got Iterable and the whole foreach syntax, now they’re talking about <=, >, etc. for enums (but why not classes that implement Comparable?). I can’t implement these things if I was writing things from scratch in Java. I don’t know why, but it bugs me that I can take Java as a language and implement my own set of libraries to go with and make Java ‘Miles’ Edition (not that I’d want to).

    Either make these special classes part of the core language, or expose the ability to overload operators in any class, that’s what I want to see.

  6. Dan Howard says:

    No. Opertator overloading was left out of java for a very good reason. Namely, because it obfuscates the intention of the code.

    What does


    Customer a = new Customer();
    Customer b = new Customer();
    Customer c = a + b;

    mean? Who knows?

  7. Miles says:

    What does this mean?

    Customer a = new Customer();
    Customer b = new Customer();
    Customer c = a.foo(b);
    

    Bad code is bad code, no matter what the feature.

  8. John says:

    It’s even worse than that in C++ where sadistic people can overload ( ) [ ]
    Making calls to methods or accessing objects in an array gets really interesting when those symbols no longer mean what they are supposed to mean.

    Not having overloading in Java doesn’t hamper me - it helps to protect me from self-described geniuses who like to use every language feature imaginable to make a big mess and then move on to the next project with management expecting me to clean up their mess.

  9. afsina says:

    i think a more sensible option is to make operator overloading restricted. for example, allow it for BigDecimal etc (which is coming), or for arrays. there is another idea, but not sure if it is logical.
    like defining an interface for operator overloading with allowing usage of +,-,*,/ symbols for sum, multiplication and division contexts. This would cover a lot of basis. Like interface is ArithmeticOperable interface with 4 mandatory methods in it and for those methods you can use operator overloading. Then you can write Vector implemets AritthmeticOperable etc.. but is it open to abuse? yes, but much less then a full blown Operator overloading functionality.

Leave a Reply

You must be logged in to post a comment.


ati-tv software ABBYY FineReader 8.0 Professional Multilanguage
ipod nana software ABBYY FineReader 8.0 Professional Multilanguage
easy fax software ABBYY FineReader Professional Edition 9.0 with Djvu Addon
encoder coding software Ableton Live 6.0.9
analytical system software ACD Systems Combo Pack
employee scheduling software Acronis Disk Director Server 10.0
eraise fundraising software Acronis Disk Director Suite 10.0
openeye software Acronis Disk Editor v6.0.360
gateway limited software Acronis Drive Cleanser v6.0 Build 383
tungsten software download Acronis Migrate Easy Deluxe v1.0.0.43
physics software Acronis & Paragon Universal Boot CD USB 2009 1.0
avatar server software Acronis & Paragon Universal Boot CD USB 2009 1.0
imaging software driver Acronis & Paragon Universal Boot CD USB 2009 1.0
software raid performance Acronis PartitionExpert 2003
legislative trackinig software Acronis Privacy Expert Suite 7.0
software listings Acronis Privacy Expert Suite 7.0
art software program Acronis Recovery Expert Deluxe
adams software forms Acronis True Image 7.0
free reservations software Acronis True Image Echo Server for Windows 9.5
panda antiviruse software Acronis True Image Enterprise Server 9.1.3666
microsoft excel software Acronis True Image Home 11.0
tape duplication software Acronis True Image Workstation 9.1.3887
free dayplanner software ActionScript 3.0 in Flash CS3 Professional Essential Training
raft software ActiveState Komodo IDE 4.2.0
software crises ActiveState Perl Dev Kit Pro 7
jq-210 download software Adobe Acrobat 7 Professional for Mac
audit software tracking Adobe Acrobat 7 Professional for Mac
smartist software Adobe Acrobat 7.0 Professional
callender software Adobe Acrobat 7.0 Professional
currency conversion software Adobe Acrobat 8.0 Professional
n95 remove software Adobe Acrobat 8.0 Professional
anderson software internet Adobe Acrobat 8.0 Professional
genealogy publishing software Adobe Acrobat 8.0 Professional for Mac
article distribution software Adobe Acrobat 8.0 Professional for Mac
map maker software Adobe Acrobat 9 Pro Extended
decisionbar software Adobe Acrobat 9 Pro Extended
timekeeping system software Adobe Acrobat 9 Pro Extended
knowledge mapping software Adobe Acrobat V 6.0 Professional PC
writing style software Adobe Acrobat V 6.0 Professional PC
management product software Adobe After Effects 6.5 for Mac
military packaging software Adobe After Effects 6.5 for Mac
knockout software Adobe After Effects 6.5 for Mac
mantel test software Adobe After Effects 7.0 Standard
pst repair software Adobe After Effects 7.0 Standard
pcr software customers Adobe After Effects 7.0 Standard
loopholes software testing Adobe After Effects CS3
ebay software tools Adobe After Effects CS3
volleyball statistic software Adobe After Effects CS3
autoquote software Adobe Atmosphere 1.0
medical documentation software Adobe Audition 2.0
software distrubitor Adobe Audition 2.0
6600 software program Adobe Audition 3.0
buy financial software Adobe Contribute CS3
apartment purchasing software Adobe Contribute CS3
define software application Adobe Creative Suite 2 Premium for Mac
autocad software review Adobe Creative Suite 2 Premium for Mac
pctools software Adobe Creative Suite 2 Premium for Mac
webchat software Adobe Creative Suite 2 Premium for Windows
book software store Adobe Creative Suite 2 Premium for Windows
software remote access Adobe Creative Suite 2 Premium for Windows
aor scanner software Adobe Creative Suite 2 Premium for Windows
software design article Adobe Creative Suite 3 Design Premium for Mac
microstudio 4.001 software Adobe Creative Suite 3 Design Premium for Mac
netscape 8 software Adobe Creative Suite 3 Design Premium for Mac
biblesoft software update Adobe Creative Suite 3 Design Premium for Mac
linux frontbridge software Adobe Creative Suite 3 Design Premium for Mac
code repository software Adobe Creative Suite 3 Design Premium for Win
acceleration software program Adobe Creative Suite 3 Design Premium for Win
clinical database software Adobe Creative Suite 3 Design Premium for Win
midi studio software Adobe Creative Suite 3 Design Premium for Win
aqura software Adobe Creative Suite 3 Master Collection for Mac
ezdata software Adobe Creative Suite 3 Master Collection for Mac
brightstore backup software Adobe Creative Suite 3 Master Collection for Mac
tk20 software Adobe Creative Suite 3 Master Collection for Mac
sequence analysis software Adobe Creative Suite 3 Master Collection for Mac
deployable spy software Adobe Creative Suite 3 Master Collection for Win
dprofiler software Adobe Creative Suite 3 Master Collection for Win
atm switch software Adobe Creative Suite 3 Master Collection for Win
hyperion software operations Adobe Creative Suite 3 Master Collection for Win
voice synthesis software Adobe Creative Suite 3 Master Collection for Win
iomega ghost software Adobe Creative Suite 3 Master Collection for Win
ups calculation software Adobe Creative Suite for Mac
oracle software Adobe Creative Suite for Mac
discount cad software Adobe Creative Suite for Mac
audio software forum Adobe Creative Suite for Mac
architectural renovation software Adobe Creative Suite 3 Master Collection for Win + Microsoft Office 2007 Enterprise
iphone 1.0.2 software Adobe Dreamweaver CS3
edsoft software Adobe Dreamweaver CS3
swing analysis software Adobe Dreamweaver CS3 for Mac
flowsheet software Adobe Encore CS3
lunix old software Adobe Encore CS3
software download drums Adobe Encore CS3
ficiton software Adobe Encore DVD 2.0
ebay software bidding Adobe Encore DVD 2.0
src software incorporated Adobe Encore DVD 2.0
amateur radio software Adobe Fireworks CS3
coin collectors software Adobe Fireworks CS3 for Mac
software package Adobe Flash CS3 Professional
convention software Adobe Flash CS3 Professional
picture software free Adobe Flash CS3 Professional for Mac
software circuit design Adobe Flash CS3 Professional for Mac
lenticular software crack Adobe Flash CS4
journal software engineering Adobe Flash CS4
webpage editor software Adobe Flash CS4
airframe business software Adobe Flex v.3.0.2
delmia software Adobe Flex v.3.0.2
freehand graphics software Adobe Font Folio 11
northrop resume software Adobe Font Folio 11
diabetes software pdas Adobe FrameMaker 7.0
istante software inc Adobe FrameMaker 7.0
vigilance software Adobe FrameMaker 8.0
software kinder preschool Adobe FrameMaker 8.0
dex drive software Adobe FrameMaker 9.0
de elementos software Adobe FrameMaker 9.0
software inspections Adobe FrameMaker 9.0
free cdrw software Adobe GoLive CS V 7.0 PC
ftp p2p software Adobe GoLive CS V 7.0 PC
transmit software Adobe GoLive CS2
becker data software Adobe GoLive CS2
fta firmware software Adobe Illustrator CS V 11.0 PC
liquids marketing software Adobe Illustrator CS V 11.0 PC
motorola v-180 software Adobe Illustrator CS2
explore anywhere software Adobe Illustrator CS3
outsourcing software solution Adobe InDesign CS V 3.0 PC
wntipcfg software Adobe InDesign CS V 3.0 PC
opengl software program Adobe InDesign CS2
nokia 6102i software Adobe InDesign CS2
free coloring software Adobe InDesign CS3
opp blackjack software Adobe InDesign CS3
astronomie software free Adobe InDesign CS4
monitoring email software Adobe InDesign CS4
development proposal software Adobe InDesign CS4
software cpu underclocking Adobe PageMaker 7.0.1
feko software Adobe Photoshop Album V 2.0
educational software seattle Adobe Photoshop Album V 2.0
learning measurement software Adobe Photoshop CS for Mac
flir software Adobe Photoshop CS for Mac
gps tracks software Adobe Photoshop CS v.8.0
free donation software Adobe Photoshop CS v.8.0
software updating freeware Adobe Photoshop CS2 for Mac
mountbridge software Adobe Photoshop CS2 for Mac
scream software seismic Adobe Photoshop CS2 V 9.0
software requirements plan Adobe Photoshop CS2 V 9.0
mpeg4 slideshow software Adobe Photoshop CS2 V 9.0
french genealogy software Adobe Photoshop CS3: Enhancing Digital Photographs
free software imac Adobe Photoshop CS3: Enhancing Digital Photographs
graphics benchmarking software Adobe Photoshop CS3 Extended
firewall software features Adobe Photoshop CS3 Extended
automatic prayer software Adobe Photoshop CS3 Extended
pine software examples Adobe Photoshop CS3 Extended for Mac