more progress on traits

This commit is contained in:
Alex Tkachman
2011-09-12 21:23:42 +03:00
parent 4c5d74ea93
commit cf639cf084
9 changed files with 186 additions and 25 deletions
+25 -1
View File
@@ -1,10 +1,16 @@
class A() {}
class B<T>() {
open class B<T>() {
fun isT (a : Any?) : Boolean {
return a is T
}
}
class C() : B<String>() {
}
class D<T>() : B<B<T>>() {
}
fun t1() : Boolean {
val a = A()
if(a !is A) return false
@@ -148,6 +154,18 @@ fun t24 () : Boolean {
return true
}
fun t25 () : Boolean {
val c = C()
if(!c.isT("aaa")) return false
return true
}
fun t26 () : Boolean {
val d = D<String>()
if(!d.isT(B<String>())) return false
return true
}
fun box() : String {
if(!t1()) {
return "t1 failed"
@@ -221,5 +239,11 @@ fun box() : String {
if(!t24()) {
return "t24 failed"
}
if(!t25()) {
return "t25 failed"
}
if(!t26()) {
return "t26 failed"
}
return "OK"
}
+15
View File
@@ -0,0 +1,15 @@
trait AL {
fun get(index: Int) : Any? = null
}
trait ALE<T> : AL {
fun getOrNull(index: Int, value : T) = get(index) as? T ?: value
}
class SmartArrayList() : ALE<String> {
}
fun box() : String {
val c = SmartArrayList()
return if("239" == c.getOrNull(0, "239")) "OK" else "fail"
}
@@ -0,0 +1,15 @@
open class AL<T> {
fun get(index: Int) : T? = null
}
trait ALE<T> : AL<T> {
fun getOrValue(index: Int, value : T) : T = get(index) ?: value
}
class SmartArrayList() : ALE<String>, AL<String> {
}
fun box() : String {
val c = SmartArrayList()
return if("239" == c.getOrValue(0, "239")) "OK" else "fail"
}