Sunday, January 17, 2010

Is Scala more complicated than what Java tries to become?

Is Scala more complicated than Java? My last post did not tell the whole truth. I've only listed Scala features, which have a Java analog. There is a glaring omission of advanced Scala features like implicit conversions, operator overloading, call-by-name parameters and pattern matching. These Scala features are more complicated than what Java has. There, I said it. But then Scala is more complicated in the way a calculator is more complicated than an abacus- sure you can do some of the same stuff with an abacus, but trying to calculate the square root of a number is much more cumbersome.

However, this complexity pays off, because it lets us simplify many day-to-day features. This post will try a different angle by comparing where Java wants to be and where Scala is right now. I hope after reading it you will at least question your assumptions whether this trade-off is worth it.

Upon its creation, Java was a fairly simple language. A major reason it took over C++ is because it was specifically designed to steer away from multiple inheritance, automatic memory management and pointer arithmetic. But it's not a simple language anymore, and it's getting more and more complicated.

Why? Java wasn't designed to be too extensible. Scala, on the other hand, was designed to be scalable, in the sense of flexible syntax. The very creators of Java knew very well that a "main goal in designing a language should be to plan for growth" (Guy Steele's famous words from Growing a Language)

We need to expand our definition of language complexity. The language needs to be able to abstract away accidental complexity, or using it will be difficult. Examples of accidental complexity: jumping to a position in your program with goto, and then remembering to go back (before procedural programming); or allocating memory, and then remembering to deallocate it (before garbage collectors). Another example: using a counter to access collections, and remembering to initialize and increment it correctly, not to mention checking when we're done.

Creating extensions of the language in order to hide these complexities doesn't happen often. When it does, it offers huge rewards. On the other hand, if a language is rigid, even though it looks simple, this forces you to invent your own arcane workarounds. When the language leaves you to deal with complexity on your own, the resulting code will necessarily be complicated.

Let's see what special new language features Java tries to add to the language, which Scala can do because of its flexibility and extensibility.

Pattern matching

Pattern matching is often compared with Java's switch/case statement. I have listed pattern matching as something which doesn't have an analog in Java, because comparing it to "switch" really doesn't do it justice. Pattern matching can be used for arbitrary types, it can be used to assign variables and check preconditions; the compiler will check if the match is exhaustive and if the types make sense. Meanwhile Java has only recently accepted Strings in switch statements, which is only scratching the surface of Scala's pattern matching.

Furthermore, Scala is using pattern matching all through the language- from variable assignment to exception handling. To compare, the proposal for handling multiple exceptions in Java is postponed yet again.

Case classes

In order to get rid of Java's verbose getters, setters, hashCode and equals, one solution is to muck with the javac compiler, like the folks from Project Lombok have done. Is going to the guts of javac complicated? I'm sure it is.

In Scala, you can do it if you just define your classes as case classes.

Implicit conversions

In short, implicit conversions help transparently convert one type to another if the original type doesn't support the operations requested.

There are many examples where this is useful.

What in Java is hardcoded in the language as conversions and promotions, in Scala is defined using implicit conversions. This is another example where Java can get quite complicated. In most cases where you need to decide how to convert a method argument, for instance, you must have in mind narrowing and widening conversions, promotions, autoboxing, varargs and overriding (whew!). In Scala, the advantage of having implicit conversions is that you can inspect the code, where no ambiguity can result. You can analyze the conversions taking place in the interpreter by supplying the "-Xprint:typer" parameter. You can even disable these implicits, if you don't like them, by shadowing the import.

Another example of what implicits can do is adding methods and functionality to existing classes. Dynamic languages already do that easily using open classes and "missing method" handlers. In Java one way to do this using bytecode manipulation trickery via libraries like cglib, bcel, asm or javassist.

Bytecode manipulation in Java is required for popular libraries like Hibernate, Spring and AspectJ. Few "enterprise" Java developers can imagine development without Hibernate and Spring. Although there are many more things you can do with AspectJ, it can be used to emulate implicits with type member declarations. However, even though using AspectJ is a more high-level way to solve the problem, it adds even more complexity, as it defines additional keywords and constructs.

If you're new to Scala, you don't lose much if you don't know how implicit conversions work, just like you don't need to know about the magic that happens behind the scenes when Hibernate persists objects or when Spring creates its proxies. Just as with bytecode generation, you're not advised to use this feature often, as it is difficult to use. Still, you'll be glad it exists, because someone will create a library which will make your life and the life of many developers so much easier.

Operator overloading

The line between operators and methods in Scala is blurred- you can use the symbols +, -, /, *, etc. as method names. In fact, that's exactly how arithmetic operators work in Scala- they are method invocations (relax, everything is optimized by the compiler).

Some people object that operator overloading adds unnecessary complexity, because they can be abused. Still, you can also abuse method naming in much the same way. For instance, some hapless folk can define methods with visually similar symbols, like method1, methodl and methodI. They can use inconsistent capitalization, like addJar or addJAR. One could use meaningless identifiers like ahgsl. Why should operator best practices be different than method naming best practices?

What is complicated is treating numeric types like ints and BigInteger differently. Not only that, but operations with BigInteger are very verbose and barely readable even with simple expressions. To compare, this is how a recursive definition of factorial looks like in Scala with BigInteger:

def factorial (x: BigInt): BigInt =
if (x == 0) 1 else x * factorial(x - 1)

This is how it would look if Scala didn't support operator overloading:

def factorial (x: BigInteger): BigInteger =
if (x == BigInteger.ZERO)

Call by name

One of the proposals for Java 7 language extension was automatic resource management. This is one more rule to the language, which you need to remember. Without this feature, code is also unnecessarily complicated, because it forces you to remember to always close resources after using them- if you slip up, subtle bugs with leaking files or connections can result.

In Scala, it's easy to add language constructs like this. Using function blocks, which are evaluated only when they are invoked, one can emulate almost any language construct, including while, if, etc..

Existential types

Existential types are roughly an alternative to Java wildcards, only more powerful.

Martin Odersky: If Java had reified types and no raw types or wildcards, I don't think we would have that much use for existential types and I doubt they would be in Scala.

If Martin Odersky says that existential types wouldn't be in the language if it wasn't for Java compatibility, why would you even need to know about them? Mostly if you need to interoperate with Java generics.


Scala tries to define fewer language rules, which are however more universal. Many of these advanced features are not often used, but they pay off by allowing to create constructs, which in Java would require specific hardcoded additions to the language. In Scala, they can be defined simply as libraries.

Why does it matter that it's in the libraries, and not hardcoded in the language? You can more easily evolve and adapt these features, you can add your own extensions, and you can even disable some of the library parts or replace them.

The conclusion is that if a language is not designed to be extended, it will eventually develop features, which are not well-integrated and this language will collapse under the weight of its own complexity.

Finally, learning something so that you avoid a lot of routine error-prone operations reduces effort by increasing the level of abstraction, at the cost of additional complexity. When you were in school, it was complicated to learn multiplication, but if you got over it, it would save you from quite a bit of repetition than if you just used addition.

P.S. I realize it's not possible to resolve the issue once and for all which language is more complicated- Java or Scala- in a post or two. First of all, have in mind that simple is not the same as easy to use. There are also many topics which are open for discussion. I haven't touched on Scala traits; I haven't mentioned functions as first-class constructs compared to the Java 7 closure proposal; and there's a lot that can be said about how Scala obviates many Java design patterns. Extending the Scala syntax via compiler plugins is another interesting advanced topic.

I suppose someone could even write a blog post about these topics some day.


younger idea said...

Get easy Free Hindi Ebooks of Programming Languages PHP,JAVA, HTML5 and CSS3,JAVASCRIPT,JQUERY,C,C++,C#(SHARP),DATA STRUCTURE,VB6 etc.

Get,Learn & Do & Earn Huge Income.
For get the ebooks free in pdf format,click on below link:

Free Downloads Ebooks of Programming Languages in Hindi in pdf format

peterson said...

Thanks for sharing lot of information about programming languages. Your article is really informative. Also I want you to share more information related with programming languages like c, c++. Can you update it in your website?
java training in Chennai

jack wilson said...

Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. It is really very helpful for us and I have gathered some important information from this blog.

JAVA J2EE Training in Chennai

sydney jones said...

Thanks for sharing the information about programming languages.


Java Course in Chennai

dhanamlakshmi palu said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai

Dagdgsd Dffbd said...

Nice article, thanks for the information. It's very complete information. I will bookmark for next reference
jaring futsal | jaring golf | jaring pengaman proyek |
jaring pengaman bangunan | jaring pengaman gedung

Bally Uppal said...

Get easy Free Hindi Ebooks of Programming Languages PHP,JAVA, HTML5 and CSS3,JAVASCRIPT,JQUERY,C,C++,C#(SHARP),DATA STRUCTURE,VB6 etc.

Get,Learn & Do & Earn Huge Income.
For get the ebooks free in pdf format,click on below link:

Bally Uppal said...

Get all Computer Books in Hindi

Download all computer ebooks in Hindi

Bally Uppal said...

Get all Computer Books in Hindi

Download all computer ebooks in Hindi

Andrew Son said...

The information you have given here are most worthy for me. I have implemented in my training program as well, thanks for sharing.

Big Data Training in Chennai
Big Data Course in Chennai

Amirtha rao said...

Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
Informatica training in chennai|Best Informatica Training In Chennai

varshini devi said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
SAP training in chennai|SAP ABAP Training in Chennai|SAP Training in Chennai|SAP training

Yasmin Priya said...

Thanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.
Thanks & Regards
Big Data Training in Chennai | Big Data Training in Chennai

akmal niazi khan said...

Programming is very interesting and creative thing if you do it with love. Your blog code helps a lot to beginners to learn programming from basic to advance level. I really love this blog because I learn a lot from here and this process is still continuing.
Love from Pro Programmer

Anonymous said...

its very helpful to know about your blog. and its very much useful for us to get knowledge from yours. thanks for sharing.

Vasu N said...

Thanks for sharing with us that awesome article you have amazing blog....

Rakesh S said...

nice posts..
SAS Institute introduced the SAS Certified Professional Program,training proper understanding of how the SAS software works. Among the five certification programs that SAS Institute has come up with, SAS training can be considered as the entry point into the big data and the data analytics industry.
SAS online training in hyderabad

Rakesh S said...

Thanks for sharing this nice article..
SAS Institute introduced the SAS Certified Professional Program,training proper understanding of how the SAS software works. Among the five certification programs that SAS Institute has come up with, SAS training can be considered as the entry point into the big data and the data analytics industry.
SAS online training in hyderabad

Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Digital Marketing Company in India
Seo Company in India

Balaji said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
Analytics Training in Chennai

sapna negi said...

thanks for sharing this .Nice article
best sap abap institute

Philips Huges said...

Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

Personal Installment Loans
Payday Cash Advance loan
Title Car loan
Cash Advance Loan

Unknown said...

Thanks for sharing this good blog.It's amazing blogJava Online Course

genesis sarah said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Best Hadoop Training Institute In chennai


Brain Carve said...

Thanks for sharing..
Education Franchise Business Opportunities
Low Cost Franchise Opportunities in chennai
franchise opportunities
franchise business
education franchise opportunities
franchise opportunities in chennai

Brain Carve said...

nice post..
Abacus Training Class in Chennai
Vedic Maths Classes in Chennai

Clinnovo said...

Thanks for sharing Good Information
Online SAS Training

Arushi Chaya said...

I have really happy to these reading your post. This product control and maintenance of our health.The daily routine can assist you weight lose quickly and safely.My life is completely reworked once I followed this diet.I feeling nice concerning myself.

Herbalife in Chennai
Herbalife Nutrition Products
Nutrition centers in Chennai
Weight Loss in Chennai
Weight Gain in Chennai

aruna raj said...

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you article.
Big data training in Velachery
Big data training in Marathahalli
Big data training in btm
Big data training in Rajajinagar
Big data training in bangalore

pooja saravanan said...

I'm here representing the visitors and readers of your own website say many thanks for many remarkable
MEAN stack training in Chennai
MEAN stack training in bangalore
MEAN stack training in tambaram
MEAN stack training in annanagar

amilu B said...

Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.

python training in chennai | python training in bangalore

python online training | python training in pune

simbu said...

Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in

java training in chennai | java training in bangalore

java online training | java training in pune

johnsy sai said...
This comment has been removed by the author.
Nila shri said...

Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post

Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore

SRI said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

rpa training in Chennai | rpa training in pune

rpa training in tambaram | rpa training in sholinganallur

rpa training in Chennai | rpa training in velachery

rpa online training | rpa training in bangalore

digi mark said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

rpa training in Chennai | rpa training in pune

rpa training in tambaram | rpa training in sholinganallur

rpa training in Chennai | rpa training in velachery

rpa online training | rpa training in bangalore

john jersy said...

Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
python training in Bangalore
python training in pune
python online training
python training in chennai

sudha P said...

This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.
java training in omr | oracle training in chennai

java training in annanagar | java training in chennai

Unknown said...

I recently came across your blog and have been reading along. I thought I would leave my first comment.

python training in velachery
python training institute in chennai

Fajlu Rahman Pvt. Ltd said...

Really amazing? keep up the work..

Naga Manickam said...

This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
Data science training in tambaram | Data Science training in anna nagar
Data Science training in chennai | Data science training in Bangalore
Data Science training in marathahalli | Data Science training in btm

johnsy sai said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
DevOps online Training
DevOps Training in USA

gowthunan said...

Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers
safety courses in chennai

dhanya sridhar said...

Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

Selenium Training in Bangalore | Best Selenium Training in Bangalore

AWS Training in Bangalore | Amazon Web Services Training in Bangalore

sathya shri said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.

angularjs Training in chennai
angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

Safety Professionals said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving. Most of ideas can be nice content.
fire and safety course in chennai

Dwarakesh babu said...

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

Oracle Training in Chennai | Best Oracle Training Institute in Chennai
Web Design Training in Chennai
Web Design Training in Chennai|Best Web Design Training in Chennai
AngularJS Training in Chennai |Advanced SAS Training in Chennai | Best SAS Training in Chennai

Anand said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Java Training in Chennai
Python Training in Chennai
IOT Training in Chennai
Selenium Training in Chennai
Data Science Training in Chennai
FSD Training in Chennai
MEAN Stack Training in Chennai

Sakthi Murugan said...

really nice blog!! with lot of recent info.Thanks for uploading.

Selenium training in Chennai
Selenium Courses in Chennai
best ios training in chennai
.Net coaching centre in chennai
French Classes in Chennai
Big Data Training in Chennai
Salesforce Course
Salesforce Developer Training

Deepika Analy said...

Very Informative article you have described everything in a brilliant way. This is really useful article for begginer learner. Appreciated!

Cloud Training
Cloud Training in Chennai
Cloud Training in OMR
Cloud Training in Velachery
Cloud Training in Thiruvanmiyur

Bee Yes said...

Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You ! Kindly Visit Us @ Coimbatore Travels | Ooty Travels | Coimbatore Airport Taxi | Coimbatore taxi

Kayal m said...

Very Amazing Blog! This very comprehensive but very easily understand to me. Really well post and very helpful for me.
big data training in bangalore
big data courses in bangalore
Big Data Hadoop Course in T nagar
Big Data Hadoop Course in Velachery
Big Data Hadoop Course in Omr
Big Data Hadoop Training in sholinganallur

Unknown said...

i found your article very intresting. really nice post. keep posting this type of important post. lots of love from rajasthan
also help me to improve my backlinking i am adding my website link.
Tramadol 50mg

vijaykumar said...

the blog is nicely maintained by author.each and every information is very useful and helpful for me.
Amazon web services Training in Chennai
AWS Training in Chennai
AWS course in Chennai
DevOps course in Chennai
DevOps Training in Chennai
Data Science Course in Chennai
Data Science Training in Chennai

gowthunan said...

Appreciating the persistence, you put into your blog and detailed information you provide.
fire and safety course in chennai

jefrin adams said...

Nicely maintain by the author

software testing training institute in chennai

sheela rajesh said...

I read your blog and i found innovative ideas in that.After a long time i had saw such kind of article.Thanks for you and i request you to update more in future.
cloud computing training in chennai
Cloud Computing Courses in Chennai
Big Data Training in Chennai
Hadoop Training in Chennai
Digital Marketing Course in Chennai
Selenium Training in Chennai
JAVA Training in Chennai
German Classes in Chennai
JAVA Training in Velachery

OLIVIA H said...

These are great points here. I like 1st para Give a pep talk. Not only will that be good for the visitors to the blog, but it is great for the bloggers mindset too!
iphone display replacement | iphone water damage service | iphone glass replacement | iphone battery replacement | 100% genuine apple parts | iphone unlocking service | | Mobile phone glass replacement

amsa leka said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angular 4 training in chennai | angularjs training in chennai

tamilselvan said...

This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot. 
devops online training

aws online training

data science with python online training

data science online training

rpa online training

jameel khan said...

I always enjoy reading quality articles by an individual who is obviously knowledgeable on their chosen subject. Ill be watching this post with much interest. Keep up the great work, I will be back
Microsoft Azure online training
Selenium online training
Java online training
uipath online training
Python online training

suresh said...

This is Very Useful blog, Thank you to Share this.

DevOps Training in Chennai

DevOps Certification in Chennai

Salesforce Training in Chennai

Microsoft Azure Training in Chennai

Priyanka said...

Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

saketh said...

I like viewing web sites which comprehend the price of delivering the excellent useful resource Python classes in pune free of charge. I truly adored reading your posting. Thank you!