class

All Scala classes are derived from Object(from Java)

Declaration

class Rational(x: Int, y: Int) {
    require(y > 0, "y must be positive") //basically assert but different exception

    def numer = x
    def denom = y
    private val g = 3

    def this(x: Int) = this(x, 1) //constructor overloading

    override def toString = "X: " + x + " Y: " + y
    private[this] def myF() {
        //.....
    }
}

Usage

val x = new Rational(1, 2)
x.numer
x.denom

Case Classes

convenientaly store and match on class contents(i.e insted of if comp.brand == "HP")

Basically extras on top of class that can be used like a class

  • Getters, no setters

  • generates hashcode, toStr, and equals

  • ez copying hp20b.copy(brand = "Texas")

Class Modifiers

abstract

cant be instated with new and can have unimplemented its

Sealed & Final

final can't be extended anywhere and sealed can only be extended in the same Scala file

Generics

Ez Overloading

Usage

Soooo you can have very natural operator overloading on +

precedence defined as first character so ez i.e a+bc == a+(b\c) regardless of what a, b, and c are

Variance

Basically allows you to answer question if B is subclass of A is Container[B] a subclass of Container[A]

Look up more if you need this

Last updated