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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package test
import annotation.{tailrec, switch}
import collection.mutable._
object typers {
val names = List("a", "b", "c")
val ints = List(1, 2, 3)
for ((name, n) <- (names, ints).zipped)
println(name.length + n)
val entries = Array("abc", "def")
for ((x, i) <- entries.zipWithIndex)
println(x)
object Eta {
def fun(x: Int): Int = x + 1
val foo = fun(_)
}
case class DefaultParams(init: String => String = identity)
object DefaultParams {
def foo(x: String => String = identity) = x("abc")
foo()
}
class List[+T] {
def :: (x: T) = new :: (x, this)
def len: Int = this match {
case x :: xs1 => 1 + xs1.len
case Nil => 0
}
}
object Nil extends List[Nothing]
case class :: [+T] (hd: T, tl: List[T]) extends List[T]
def len[U](xs: List[U]): Int = xs match {
case x :: xs1 => 1 + len(xs1)
case Nil => 0
}
object returns {
def foo(x: Int): Int = {
return 3
}
}
object tries {
val x = try {
"abc"
} catch {
case ex: java.io.IOException =>
123
} finally {
println("done")
}
val y = try 2 catch Predef.identity
val z = try 3 finally "abc"
println("abc".toString)
}
class C {
@tailrec def factorial(acc: Int, n: Int): Int = (n: @switch) match {
case 0 => acc
case _ => factorial(acc * n, n - 1)
}
println(factorial(1, 10))
}
class Refinements {
val y: C { type T; val key: T; def process(x: T): Int }
}
object Accessibility {
class A {
val x: String = "abc"
}
class B extends A {
private def x: Int = 1
}
val b: B = new B
val y = b.x
val z: String = y
}
object Self {
class A(self: Int) { self =>
class B {
val b = self
val c: A = b
}
val a = self
val c: A = a
}
}
object Arrays {
val arr = List("a", "b", "c").toArray
val i = 2
arr(i).charAt(0)
val x = new ArrayBuffer[String] // testing overloaded polymorphic constructors
}
object SeqExtractors {
val y = names match {
case List(x, z) => x
case List(x) => x
case List() => ""
}
val yy: String = y
}
}
|