Scala In 10 Minutes
- Pure object oriented. Everything is an object including numbers.
- single argument methods can be called like: obj method arg OR obj method { arg } OR obj.method arg OR obj.method { arg }
- Functions are objects too.
- Anonymous functions
- Classes can accept arguments ( Constructor )
- Methods can be defined without any parameters ( no parenthesis )
- To override methods specify it explicitly
- scala.AnyRef is superclass
- case classes : These are special classes which can be used in pattern matching scenarios or cases ( where we use algebraic data types in languages like Java )
- "trait" is like interface which contains code also. (Scala has abstract class too)
- Companion Object : Object having same name as class. The class then is called Companion class. Companion class and object can access each other's private members. ( static methods in companion object, instance methods in companion class.)
- All operators that end in : are right associative in Scala.
- Covariance: class List[+A] => if A is subtype of B, then List[A] is subtype of List[B]
- Contravariance: class List[-A] => if A is subtype of B, then List[B] is subtype of List[A]
- Invariance: Neither covariant or Contravariant
- Upper Bound Type: T <: A type variable T refers to a subtype of A
- Lower Bound Type : T >: A type variable T refers to supertype of A
- Compound Type : A with B with C.... ( Cloneable with Resetable )
- Self Type : someIdentifier: SomeOtherTrait => ( members of SomeOtherTrait becomes available in the currentTrait and SomeOtherTrait must be mixed into currentTrait )
- Implicit Parameters : A method can have implicit parameters, which if not passed Scala will find the implicit value of correct type and pass it.
- Implicit conversion
- Polymorphic methods : Methods can be parameterized by type as well as value. Type parameters in square brackets ( Tuple2[String,Int] )and value parameters in parenthesis.
- By-name parameters ( param : => T ) are only evaluated when used. By value parameters are evaluated only once.
- Named Arguments: you can label the arguments with parameter names. If all the arguments are named, then you can rearrange the order. If some are not named, those have to come first in the order of method signature.
- In Scala import can occur anywhere in the source code and not necessarily at begining.
scala
andjava.lang
packages as well asobject Predef
are imported by default.- https://scalafiddle.io/ - site for experimenting scala online.
- special notation for getters/setters
- statically typed
- Monad - Monad is special kind of wrapper ( design pattern ) . Those help in writing clean code ( by hiding boilerplate code like checking for nulls etc. ) . For example Option. Options abstracts optional value ( value may or may not be there ). If it is there , it is Some(value) , if not it is None. List is a Monad too.
Reference: https://docs.scala-lang.org/tutorials/scala-for-java-programmers.html
1) java.lang.ClassNotFoundException: scala.Product$class
Below issue happened when I compile using old version of scala(2.11.0) and tried to run using 2.12.8...
java.lang.ClassNotFoundException: scala.Product$class
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.glbits.training.Var.<init>(Tree.scala:5)
at com.glbits.training.Calc$.main(Calc.scala:20)
at com.glbits.training.Calc.main(Calc.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Comments
Post a Comment