From Java to Scala
1.Use class parameters and default primary constructor. e.g.: class Rational(n: Int, d: Int){//do sth. \n def ...}
2.Use
require to check class parameters (preconditions). e.g.: require(d != 0)
3.Add "val" to the class parameters to make them as parametric fields
4.Constants named in the Java style, such as X_OFFSET, will work as Scala constants, but the Scala convention is to use camel case for constants, such as XOffset.
5.in Scala assignment always results in the unit value, ().
6.Use filter in "for" loop. e.g.:P156
7.Use "for" expression to produce a new collection directly (instead of using mutable collection)
8.Use
recursive function instead of while+break/continue
9.Fields can be overrided just like methods. And they are dynamic binding.
10.Fields can override parameterless methods.
11.Make methods with no side-effects parameterless, and call them without "()". Other methods should remain "()" even though they don't take any parameter.
12."==" <=> equals
13.Class can be both final and abstract. e.g.:Int
14.Use stackable modification(abstract class and traits) as an alternative to decorator pattern
15."import" can appear anywhere, not just at the beginning of a compilation unit
16.Importing the members of a object(either regular or singleton) is possible
17.Packages themselves can be imported
18.Imports in Scala can also rename or hide members. e.g.:P170
19.Qualified access modifiers
20."lazy": If you prefix a val definition with a lazy modifier, the initializing expression on the right-hand side will only be evaluated the first time the val is used
About trait
1.Mix in
Ordered trait to make a class comparable
2.Mixing in a trait can be done when instantiating a object with "new". e.g.:val queue = new BasicIntQueue with Doubling
3.When you call a method on a class with mixins, the method in thetrait furthest to the right is called first
4.Linearization of super calls is computed from left to right
5.When override an abstract method, "abstract" must be add. e.g.: abstract override def
Style Choice
1.infix notation should only be used for purely-functional methods (methods with no side-effects)
About Java Annotation
1.Annotations requiring an array literal of other Annotations, e.g.: @CollectionTable(name="ct", joinColumns = Array(new JoinColumn(name = "fk_id")))
About Performance
1. Create java.util.* instances and wrap them to scala mutable counterparts (import scala.collection.JavaConversions._).
2. Use Parallel Collections
3. @specialized, see
API doc
(will outperform java! by avoiding boxing/unboxing)
4. @switch in pattern matching
Other Hints:
1. Use "type" keyword to define a named type for function. E.g.:
http://stackoverflow.com/questions/1853868/how-do-you-define-a-type-for-a-function-in-scala
2. Function parameter can have default value. E.g.:
http://www.scala-lang.org/node/2075
3. Compiler will generate a "copy" method.
4. Use pattern matching to take apart Lists or Tuples
5. Traits can not have constructors. Use abstract val/var and pre-initialized fields instead
6. Decompose a string with Regex extractor (p530)
7. add "canEqual" method to achieve flexible type equality check in "equals" (p581)
8. DelayedInit trait, see
API doc
Java Interop
1. inherit scala.runtime.AbstractFuncionX to pass a function as parameter to Scala lib, where X is the number of parameters.