'Instance of a class' and 'Object' are synonym in java, but not in scala.
Scala object is a special type of class that can have only one instance.(Scala object represents a singleton class, here we don't call class but call them object.)
Scala doesn't support static members like java. A mechanism to define a static method in Scala, one must place them in an object. Generally we use Scala object for utility methods. So they can be called without even instantiating a class.
Scala Objects can extend Classes, but an object cannot be extended to create a class or another object.
Objects cannot have a public constructor.
Object will only be instantiated on their first use. Next time onwards, same instance would be used. (no use, no instance)
Scala doesn't have 'for' loop.. hmmm.. !!! then, what is 'for' loop syntax in Scala..
Scala 'for' loop is just a syntactical sugar for below control abstractions.
foreach
map
flatMap
withFilter
Constructor in Scala
Default Constructor
doesn't take any argument
class DefaultConstructor {
var variable = 0
}
Primary Constructor
take some argument
class PrimaryConstructor (var var1:Int) {
var variable = var1
}
Auxiliary Constructor
auxiliary constructors are called with 'this'. auxiliary constructor calls to a previously defined primary constructor.
Auxiliary constructors gives flexibility to create instance of class with no / fewer arguments than the PrimaryConstructor.
Apply Methods:
Apply method can be added in Class or Object. But they serve the different purposes when added in class and object.
Apply in Object
Apply in Class
scala> class Test{
| def apply(i : Int){
| println("print : " + i)
| }
| }
defined class Test
scala> val a = new Test
a: Test = Test@6bfaa0a6
scala> a.apply(10)
print : 10
So, because apply method is defined in class, so no need to specify apply here.
scala> a(10)
print : 10
Do you know this, Look at the Return type. How they are changes AnyVal, Any, Int, Option[Int].
The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left-associative.
E.g.
scala> object i {
| def ~~(j: Int) = 2*j }
defined object i
scala> i ~~ 3
res0: Int = 6
scala> object i {
| def ~:(j: Int) = 2*j }
defined object i
Below didn't work, because Operators ending in a colon ‘:’ are right-associative.
Look at the error message. (~: is not a member of Int)
scala> i ~: 3
<console>:13: error: value ~: is not a member of Int
i ~: 3
^
To make this work, we can write as below:
scala> 3 ~: i
res2: Int = 6
But, if written as below, then above rule doesn't apply.
scala> i.~:(3)
res3: Int = 6
Use lift instead of ()
scala> val l = List(8,7)
l: List[Int] = List(8, 7)
scala> l.lift(1)
res31: Option[Int] = Some(7)
scala> l.lift(6)
res30: Option[Int] = None
scala> l.lift(6).getOrElse(10)
res29: Int = 10
Case classes
Gives you: toString, equals, hashCode, Pattern matching and factory methods
'flatMap' -> to remember the flatMap, apply map first then apply flatten
abstract final class Null extends AnyRef
Null is - together with scala.Nothing - at the bottom of the Scala type hierarchy.
Null is a subtype of all reference types; its only instance is the null reference.
Since Null is not a subtype of value types, null is not a member of any such type.
For instance, it is not possible to assign null to a variable of type scala.Int.
Nothing -- is a Trait. Its a subtype of everything. But not superclass of anything. There are no instances of Nothing. Nothing has no possible value.
final trait Nothing
extends Any
null -- Its an instance of Null- Similar to Java null.
Nil -- Represents an empty List of anything of zero length.so, It refers to List which has no contents.
case object Nil
extends List[Nothing] with Product
The empty list.
None -- Used to represent a sensible return value. Just to avoid null pointer exception. Option has 2 subclasses- Some and None. None signifies no result from the method.
Do you know, None extends Option[Nothing]
case object None
extends Option[Nothing]
Unit– is analogous to a Java method which is declared void. Unit is a subtype of scala.AnyVal. There is only one value of type Unit, ().
A function that doesn't return anything must have the return type Unit. If it were Nothing then the function could not return a result. The only way to exit the function would be by an exception.
class Cheatsheet[T](val value : T){
def --[T2](f: T => T2) = {
new Cheatsheet(f(value))
}
}
Cheatsheet(2) -- {x => x+1} -- {x => x*2 }
java.lang.OutOfMemoryError: Java heap space in REPL
scala> val w = Source.fromFile("urban.geojson")(Codec.UTF8).getLines().mkString("")
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
...
Fixed By:
Starting the Scala REPL with this command to control
Jais-MacBook-Pro:~ $ scala -J-Xmx4096m
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.
scala> import scala.io.{Codec, Source}
import scala.io.{Codec, Source}
scala> val w = Source.fromFile("urban.geojson")(Codec.UTF8).getLines().mkString("")
w: String = "{"type": "FeatureCollection","crs": { "type":
....
Another Example: trait Function1[-T, +R] from the Scala standard library. Function1 represents a function with one argument, where the first type parameter T represents the argument type, and the second type parameter R represents the return type. Function1 is contravariant over its argument type, and covariant over its return type.
S <: T --- S is a subtype of T
<: T is an upper bound of the type parameter S, it means that S can be instantiated only to types that conform to T.
S >: T --- S is a supertype of T, or T is a subtype of S
S >: T1 <: T2 --- would restrict S any type on the interval between T1 and T2
call scala repl with -Xprint:typer option
combinators (e.g map)
transformer (e.g. =>)
function composition (e.g. andThen)
Today I see even scala document has cheatsheet, Good to check the link