Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

library handiness compared to groovy?

 
John Broglio
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I like about Groovy is the ease of accessing library calls. Examples from Groovy:
1. database. I can dynamically load the mysql jar, connect, and pose a query in 3 lines
2. http. I can make and process an http/https POST in a few lines (with the http-builder library)
3. mail. I can send mail in 2 lines (using AntBuilder and javamail)

In these and similar cases, how does Scala compare? I know I can drop through to Java libraries with both Groovy and Scala, but if I have to insert verbose Java-style library invocation repeatedly, that would pretty much defeat the purpose of having a succinct language.

Thanks,
John
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Groovy is a wonderful language (I love it, I use it, I've written a book on it). Groovy is concise, however,
I will not chose Groovy just because it is concise. For me, it's metaprogramming capability is very important.
So, there is more than one reason to chose a language. Conciseness is one, but it may not be the only or even
the most important one.

Similar to Groovy, you enjoy conciseness in Scala as well. You can, within a few lines of code, make
a http request and pattern match the result (Scala's pattern matching and extractors are pretty awesome).
XML is a first class citizen in Scala. This makes the code concise. Scala shares a number of features with Groovy
when it comes to conciseness. However, just like I love Groovy for more than conciseness, I love Scala for
more than conciseness as well.

To mention a few, Scala is much more strongly typed than Java and, at the same time, it has low ceremony. This is
very useful to express interface contracts without redundantly specifying types. Scala has powerful actor based model
for concurrency. This makes it easier to write correct concurrent code, and the code is concise as well.
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkat Subramaniam wrote:Groovy is a wonderful language (I love it, I use it, I've written a book on it). Groovy is concise, however, I will not chose Groovy just because it is concise. For me, it's metaprogramming capability is very important. So, there is more than one reason to chose a language. Conciseness is one, but it may not be the only or even the most important one.

Similar to Groovy, you enjoy conciseness in Scala as well. You can, within a few lines of code, make a http request and pattern match the result (Scala's pattern matching and extractors are pretty awesome). XML is a first class citizen in Scala. This makes the code concise. Scala shares a number of features with Groovy when it comes to conciseness. However, just like I love Groovy for more than conciseness, I love Scala for more than conciseness as well.

Venkat,
You mentioned metaprogramming in Groovy, but not in Scala. I though Scala supported it too. Am I wrong on that?
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Burk, Scala currently does not offer metaprogramming
capabilities like in Groovy. You can realize
some limited capabilities using traits.
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Traits? Are they a form of mixin?
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scala traits let you mixin behavior at compile time unlike Ruby/Groovy Mixins which give you the ability to mixin at runtime.
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, I'll have to do some reading... Would you happen to know a good book that might explain it?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiousity, can anyone explain in short how traits solve or avoid the problems you can get from multiple inheritance? Obviously they are more powerful than Java interface but despite this fact they don't suffer from the same problems as multiple inheritance in C++. Is this correct? Maybe some information on this? Thanks!

Marco
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco, in short you ask, so here is it in short

Object chaining.

Now for a bit more explanation. There was nothing wrong with multiple inheritance (MI). What was
wrong was how MI was implemented in some earlier languages.

The methods you implement in traits go into small objects. Depending on how you mix the traits in,
these get chained to form an instance of your class. Rather than posing an issue of method collision,
it resolves it in the way of method chaining their calls through these internal object chains.
I discuss this further in the book and show how things like decorator pattern are trivial to implement
using traits in Scala.
 
Ivano Pagano
Ranch Hand
Posts: 54
Java Mac OS X Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkat Subramaniam wrote:Marco, in short you ask, so here is it in short

Object chaining.

...

I discuss this further in the book and show how things like decorator pattern are trivial to implement
using traits in Scala.


This sounds quite interesting, could you share a simple example, if possible in a context like this short topic, of a simple decorator implementation?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkat,

thanks a lot for the "short" explanation! This helped me to find some more information on this topic in the internet. Anyway, you already convinced me to check out your book and read up on all the interesting things you summarized here in these posts! I promise

Marco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic