-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter2.kt
37 lines (32 loc) · 993 Bytes
/
chapter2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Rectangle(val height: Int, val width: Int) {
val isSquare: Boolean
get() {
return height == width
}
}
val rectangle = Rectangle(41, 43)
println(rectangle.isSquare)
interface Expr
class Num(val value: Int): Expr
class Sum(val left: Expr, val right: Expr): Expr
fun evalWithLogging(e: Expr): Int =
when (e) {
is Num -> {
println("num: ${e.value}")
e.value
}
is Sum -> {
val left = evalWithLogging(e.left)
val right = evalWithLogging(e.right)
println("sum: $left + $right")
left + right
}
else -> throw IllegalArgumentException("Unknown expression")
}
fun recognize(c: Char) = when (c) {
in '0'..'9' -> "It's a digit"
in 'a'..'z', in 'A'..'Z' -> "It's a letter"
else -> "I don't know"
}
println(recognize('8'))
println(evalWithLogging(Sum(Sum(Num(1), Num(2)), Num(4))))