Uploaded by raghul m

Scala-notes

advertisement
Scala
Friday, January 12, 2024
12:24 PM
Values, Variables and Types
1.
2.
3.
4.
val is immutable - constants
Type of val is optional, inferred by the compiler
val x:Int = 42 is similar to val x = 42
Other types
a. val aString:String = "hello",
b. val aBoolean:Boolean = true,
c. val aChar: Char = 'a',
d. val aShort: Short = 4613,
e. val aLong: Long = 534L,
f. val aFloat: Float = 2.0f,
g. val aDouble: Double = 3.14D
5. Variables are defined using var, they can be reassigned.
6. Use of variables is kind of a side effect, similar ones are printing to console.
7. Should use more val's rather than var's in functional programming.
Expressions
Expression is something that gives back a value.
Example: If expression.
val aConditionValue = if(aCondition) 5 else 3
Everything in scala is an expression.
Equivalent of void in scala is Unit
val aWeirdValue = (aVariable = 3). Printing aWeirdValue will return () which is the value of Unit.
Sideeffects in scala return unit. For example, reassigning a value, while loop etc.
Anything written in between {} is a code block. The last code line in the code block will be the return type of this expression.
Val checkType = {
Val x = 3
if(aCondition) 5 else 3
}
The type of checkType is Int
Functions
def afunctoin(aString: String, n:Int): String = {
if (n == 1) aString
else aString + afunction(aString, n-1)
}
Use functions instead of loops. Scala compiler can generally identify the return types, but not for recursive functions. Better to
provide to be clear. Code blocks can also define a function.
Type Inference
Compiler in most cases identify the type of function and also a variable. In recursive functions it fails, hence code should explicitly provide the
return type.
Call by Name & Call by Value
def callByValue(x: Long): Unit = {
Quick Notes Page 1
def callByValue(x: Long): Unit = {
println("call by value " + x)
println("call by value " + x)
}
def callByName(x: => Long): Unit = {
println("call by name " + x)
println("call by name " + x)
}
callByValue(System.nanoTime())
callByName(System.nanoTime())
Default and named arguments
1. Pass in every leading argument
2. Name the arguments
String interpolators
s interpolator and f interpolator
s" $name age is ${age + 1}"
f"I can eat $num%2.2f burgers" - f interpolators also can check for type correctness.
raw"This is a \n newline" - raw interpolator will print the string as it is, \n will be printed as \n
Instead if you are injecting lets say
val str = "this is a \n new line"
println(raw"$str") - this wont print the \n.
OO Basics
class Person is the simplest example of a class
class Person(name: String, age: Int) this is a constructor. Remember the parameters inside are class
parameters. But in the above example they are not fields.
To convert them to fields do this
class Person( val name: String, val age: Int), then you can access this by
val p = new Person("John", 38)
println(p.name + " " + p.age)
Class can have body as well
class Person(name: String, age: Int) {
val x = 2
println("hiello")
}
The items inside the code block is the value of the code block which is the implementation of the class, which
is ignored. You can do anything here.
The val and var definitions inside class implementation are fields, you can access it from the object.
When methods are defined, and there is a conflict in the parameters, then use this keyword.
Class Person(name: String, age: Int) {
def greet(name: String): Unit = println(s"${this.name} says hi")
Quick Notes Page 2
def greet(name: String): Unit = println(s"${this.name} says hi")
}
Using this will access the class parameter and need not be a field.
Multiple constructors can be provided using this
Class Person(name: String, age: Int) {
def this(name: String) = this(name, 0) // only issue with these constructors is a constructors implementation
can only called another constructor.
}
Class parameters can also take default parameters.
Method Notations
There are 3 types of method notations
1. Infix: mary + "scala", any operator can be used in method names. Infact 2 + 3 = 2.+(3)
2. Prefix - examples unary_, only few operators are allowed. +, - , ~ and !
3. Postfix: no paramter methods can be called using postfix notation. E.g mary.learn, where learn is def
learn: String = s"hi"
4. Apply method is an interesting method, that you can add to any class, and lets say
class Person(val name: String) {
def apply(): String = s"$name"
}
val person = new Person("Mary")
then println(person) would print Mary.
Scala Objects
Quick Notes Page 3
Download