Single Abstract Method
trait Action {
def act(x: Int): Unit
}
// 다음과 같이 사용해야 하지만
val action: Action = new Action {
override def act(x: Int): Unit = println(x)
}
// 아래와 같이 표현하는 것도 가능
val action: Action = (x: Int) => println(x)
// 비슷하게 아래와 같이 Thread 생성 가능
val thread: Thread = new Thread(() => println("run"))
Methods with ':' are special
class MyStream[T] {
def -->:(value: T): MyStream[T] = ???
}
val stream = 1 -->: 2 -->: new MyStream[Int]
Infix Generic Types
trait <[A, B]
def lessThan[A, B]: A < B = ???
Rewrite to Update
val arr = Array(1, 2, 3)
arr(2) = 7 // arr.update(2, 7)
arr.foreach(println) // 1 2 7
Setters for Mutable Types
class Mutable {
private var internalMember: Int = 0
def member: Int = internalMember
def member_=(value: Int): Unit = internalMember = value
}
val mutable = new Mutable
mutable.member = 3
println(mutable.member) // 3
Unary Operator
class MyString(value: String) {
def unary_! : String = value.reverse
}
val s = new MyString("abcd")
val reverse = !s
println(reverse) // dcba
참고: https://www.udemy.com/course/advanced-scala/