views:

525

answers:

4

I just found this web page comparing some code written in Scala, C# and Go. I am astonished to see how close Scala and Go code looks like, much more than Scala code compared to C# code.

So my question is: What are the most significant differences between Scala and Go?

+7  A: 

I don't think so, at least conceptually they are very different. Go is much more C++ like and "low level" oriented than C#, and C# has only a few functional features while Scala allows you to write typical "functional" Code (more verbose than OCaml or Haskell, but similar). Scala's type system is quite sophisticated, but is nevertheless based on a Java/C#-like foundation. On the other hand, Go's object-oriented part looks quite different. I didn't try out Go, as I found it too C-like and too low-level. As a Java programmer I know that C# is technically ahead and has a lot of nice features missing in Java. When learning Scala it widened my view, giving me the advantage and power of functional Code without losing the good things from the object oriented world. After the years in Java jail programming in Scala is a really refreshing and mindblowing experience.

Landei
On Wikipedia the first sentence is "Scala is a multi-paradigm programming language ..." So, as you say, it allows you to write typical "functional" code but there is nothing wrong if you prefer an imperative style. And I agree, the real name of Go is C2.
John
+1  A: 

I've seen some people almost directly convert Java code to Go code, C code to Go code, etc. I now see you can almost directly convert Scala code to Go code. When languages have a similar heritage, that's often not hard to do. However, the comparison should be between code written specifically to take advantage of the idioms of a particular language. I recently looked at some pointer dependent C code converted almost directly to Go code. It was not only painful to read; it was also painfully slow.

And, to complement MJP's example of the Sieve of Eratosthenes in Scala, look at the Prime numbers section of A Tutorial for the Go Programming Language for a detailed description of an idiomatic concurrent programming implementation of the Sieve of Eratosthenes in Go.

peterSO
+18  A: 

Honestly, that Scala code is written in an extremely imperative style. I'm no functional purist, but contrast that page's Sieve of Eratosthenes code with the much shorter, more legible Sieve of Eratosthenes example at the beginning of the Scaladoc for the Stream class. That page's code has got loads of vars and while-loops, not to mention bitshifts, all over the place. Now, I don't know how much of a consensus there is about what's considered idiomatic Scala code, but this is most definitely nowhere near it. As such, it doesn't say anything about the similarity between Scala and Go other than the fact that they're both descendants of C.

MJP
Honestly, did you noticed that your 'answer' doesn't say anything about my question?
John
Did you notice that you prefaced your question with a laughable comparison between Scala and Go based on some very lousy code? Your preface to your question is what really needed addressing.
MJP
+3  A: 

Go is just another imperative language which is still in diapers. As of now, Go doesn't have generics. Also there's no support for functional programming.

C# is an OO / imperative language with a very little support for functional programming. Has Generics. Current version doesn't support Covariance and Contravariance annotations (though they're planned for the next version of the language).

Scala is a hybrid language that tries to combine the best of both worlds (namely, OO and functional) into one language. As can be seen from the following figure (Source: http://james-iry.blogspot.com/2010/05/types-la-chart.html), Scala has got a very sophisticated file system, something that both C# and Go lack.

alt text

So featurewise, Scala is the most feature-rich language (considering both OO and functional features) of the three. C# does provide some functional constructs but it's nowhere close to Scala. And IMO comparing Go with Scala / C# is like comparing bullock cart with a Lamborghini.

Jay Sinha
If I were to place Go on the above graph then I'd place it a little to the right of C. Vertical position would remain the same.
Jay Sinha
@Jay: It's absurd to use a bullock cart through Lamborghini rating scale for programming languages. Programming languages are tools, and, like any tool, they should be rated based on their fitness for a particular purpose. If you want to clear land of boulders and trees, use a bullock or an elephant, not a Lamborghini. When I cross an ocean, I prefer a Boeing to a Lamborghini. Most days, I use around 3 to 6 different languages, using the best language for a given purpose.
peterSO
@peterSO: _"If you want to clear land of boulders and trees, use a bullock cart or an elephant,"_ <-- Think of low level programming as a land with boulder and trees here. :)
Jay Sinha
@peterSO: Still I admit it's a bad analogy. Can you suggest something better?
Jay Sinha
Scala's type system is certainly more powerful than C#'s, but the current version of C# (4.0) does support co- and contra-variance (for delegates and interfaces). While not as good as Scala's, C#'s support for functional programming is also decent; delegates are the equivalent of first class functions and many .NET libraries make good use of them.
kvb
Why the downvote?
Jay Sinha
You say that Scala is the most "feature-rich" of these three languages. However, C# is probably bigger in the number of features it has - it's the king of all kitchen-sink languages. Martin Odersky has said that he's aiming for a small set of powerful, orthogonal features rather than throwing in a bunch of trivial ad-hoc features like structs and first-class events and ref/out parameters and such. For example, where C# 3.0 adds a bunch of LINQ keywords to the language itself, Scala just writes map/filter/foreach/etc. methods for its collections that take functions as parameters.
MJP
@MJP: That's true. Scala's one feature easily amounts for ~10 features in C#. For example: Scala's flexible method naming syntax amounts to following features in C#: methods, properties, collection initializers, indexers, operator overloading etc. What a great language Scala is!
Jay Sinha