I recently bought Programming Scala, and have been reading through it. The language definitely isn't what I expected! Specifically, it seems to implement just about every programming language idea I'm aware of, outside of Lisp macros and Haskell's type-level side-effect segregation.
Frankly, it has me somewhat overwhelmed. Though I suppose it's nice to have so many tools at my disposal, I was really just looking for a strongly-typed functional language on the JVM. I imagine I could probably use Scala that way, but I imagine if I interact with any libraries or go through anyone else's code, I'll be running into a lot of this advanced (for me) OOP stuff--traits and "object hierarchy linearization," all this abstract and overriding business, singleton, package, and companion objects, implicit conversions... not to mention the various syntactic shortcuts and sugars.
People often bemoan programmers who try to shoehorn one language's style into another, for lots of good reasons. But not all languages are as multi-paradigm as Scala, so perhaps its community has a different view? In F#, for example, there seems to be some leeway in programming styles and how much OOP you use. But just from reading I'm not sure if this is a good philosophy for Scala as well.
Can more experienced Scala programmers help me out here? Edit for clarity: Basically, can I safely use just (or mostly) the FP features of Scala without worrying about its advanced OOP side?
Sorry for the rambling question!
Efficiency issuesFunctional programming languages are typically less efficient in their use of CPU and memory than imperative languages such as C and Pascal. This is related to the fact that some mutable data structures like arrays have a very straightforward implementation using present hardware.
Scala is a language that features full support of Functional Programming as well as the Object-Oriented Paradigm. It is one of the most widely utilized languages by the Functional community, up to par with F#, and Haskell, Clojure, among others.
The Advantages of ScalaScala has an exact syntax, eliminating boilerplate code. Programs written in Scala require less code than similar programs written in Java. It is both an object-oriented language and a functional language. This combination makes Scala the right choice for web development.
It is used to implement concurrency/parallelism because pure functions don't change variables or any other data outside of it. It adopts lazy evaluation which avoids repeated evaluation because the value is evaluated and stored only when it is needed.
One of the things you're seeing is that Scala is, above all, a strongly-typed functional language on the JVM
Scala is not just a Functional language. It did start off as one (Funnel: http://lamp.epfl.ch/funnel/), but this was then extended to make it an Object-Oriented language, with the explicit goal of making it strongly interoperable with Java classes.
abstraction and overriding and packages are all examples of this interoperability in action.
The remainder might be considered not as new features, but simply as removing restrictions from Java. Taking them one at a time:
traits and object hierarchy linearization
Removes the restriction that Java interfaces can only contain abstract methods. Linearisation is how Scala resolves the diamond-inheritance problems that this would otherwise cause.
singletons
Java static methods are a hangover from it's C++ syntax heritage, which in turn added them to better support the procedural style need for interop with C. Static methods are very much NOT object oriented (see this question: Why are singleton objects more object-oriented?)
companion objects
Allow singletons to be used in lieu of static methods, with their privileged access rights.
implicit conversions
Java already does this, but it's restricted to only implicitly converting objects and primitives to strings, as in the expression "" + 3
. Scala just extends this idea and allows the programmer to use it for other conversions.
Let me add a few comments to Kevin's answer.
Traits are primarily used as abstractions (loosely speaking, the same way that Haskell type classes define abstractions) and mixins. So, my code might use the Map trait, but it's really using an instance of type like HashMap. Conversely, if I want my "Service" type to have logging functionality, I might mix in a reusable Logging trait.
Fortunately, you rarely have to think about the linearization algorithm beyond the simple cases, e.g., I can mix in a trait that intercepts (wraps) a method call to log the fact that the method was invoked. The algorithm needs to handle the more complicated linearization cases (like the examples we show in the book), but really such complicated types are a poor design, IMHO.
Singletons, including the special subset companion objects, are designed to make everything an object, but you could also just view them as namespaces wrapping functions and possibly some state.
The implicit conversions are very useful, if a bit "magical". You might find it useful to see how they are used to simulate Haskell type-classes. Here's a great post by Debasish Ghosh: http://debasishg.blogspot.com/2010/06/scala-implicits-type-classes-here-i.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With