-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyArray.scala
39 lines (26 loc) · 964 Bytes
/
polyArray.scala
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
class Thing(val data: Int) {
override def toString = data.toString
}
assert((new Thing(3)).toString == "3")
class Refined(
data: Int,
val moredata:Int
) extends Thing(data) {
override def toString = moredata.toString
def anotherString: String = "extra"
}
assert((new Refined(4, 5)).toString == "5")
assert((new Refined(4, 5)).anotherString == "extra" )
val refinedStuff: Array[Refined] = Array(new Refined(1,2), new Refined(3, 4))
assert(refinedStuff(0).toString == "2")
assert(refinedStuff(0).anotherString == "extra")
assert(refinedStuff(1).toString == "4")
assert(refinedStuff(1).anotherString == "extra")
val stuff: Array[Thing] = Array(new Thing(1), new Thing(3))
assert(stuff(0).toString == "1")
assert(stuff(1).toString == "3")
val mixed: Array[Thing] = Array(new Thing(1), new Refined(3,4))
assert(mixed(0).toString == "1")
assert(mixed(1).toString == "4")
// This fails if you try to do it:
// mixed(1).anotherString