What's so great about Scala? [closed]

2019-01-20 21:54发布

What makes Scala such a wonderful language, other than the type system? Almost everything I read about the language brings out 'strong typing' as a big reason to use Scala, but there has to be more than that. What are some of the other compelling and/or cool language features that make Scala a really useful tool?

14条回答
Explosion°爆炸
2楼-- · 2019-01-20 22:27

Here are some of the things that made me favour Scala (over, say, usual Java):

a) Type inference. The Java way of doing it:

Map<Something, List<SomethingElse>> list = new HashMap<Something, List<SomethingElse>>()

.. is rather verbose compared to Scala. The compiler should be able to figure it out if you give one of these lists.

b) First-order functions. Again, this functionality can be emulated with classes, but it's ugly.

c) Collections that have map and fold. These two tie in with (b), and also these two are something I wish for every time I have to write Java.

d) Pattern matching and case classes.

e) Variances, which mean that if S extends T, then List[S] extends List[T] as well.

Throw in some static types goodness as well, and I was sold on the language quite fast.

查看更多
我命由我不由天
3楼-- · 2019-01-20 22:27

Just shortly:

  • You get the power and platform-independency of the Java libraries, but without the boilerplate and verbosity.
  • You get the simplicity and productivity of Ruby, but with static typing and compiled bytecode.
  • You get the functional goodnesses and concurrency support of Haskell, but without complete paradigm shift and with the benefits of object-orientation.

What I find especially attractive in all of its magnificient features, among others:

  • Most of the object-oriented design patterns which require loads of boilerplate code in Java are supported natively, e.g. Singleton (via objects), Adapter, Decorator (via traits and implicits), Visitor (via pattern matching), Strategy (via closures) etc.
  • You can define your domain models and DSLs very concisely, then you can extend them with the necessary features (notification, association handling; parsing, serialization), without the need of code generation or frameworks.
  • And finally, there is full interoperability with the well-supported Java platform. You can mix Java and Scala in both directions. There is not much penalty nor compatibility problems when switching to Scala after having experienced the annoyances of Java which make code hard to maintain.
查看更多
登录 后发表回答