Add union type checks and list literal inference

This commit is contained in:
sergeych committed 2026-02-05 19:45:07 +03:00
1 parent 9db5b12c31
commit c31343a040
7 files changed
+421 -18

No files matched your search

@@ -18,6 +18,7 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertFailsWith
class TypesTest {
@@ -166,4 +167,69 @@ class TypesTest {
""".trimIndent()
)
}
@Test
fun testIsUnionIntersection() = runTest {
eval("""
class A
class B
class C: A, B
val c = C()
assert( c is A | B )
assert( c is A & B )
assert( !(c is A & String) )
val v = 1
assert( v is Int | String | Real )
assert( !(v is String | Bool) )
""".trimIndent())
}
@Test
fun testListLiteralInferenceForBounds() = runTest {
eval("""
fun acceptInts<T: Int>(xs: List<T>) { }
acceptInts([1, 2, 3])
val base = [1, 2]
acceptInts([...base, 3])
""".trimIndent())
assertFailsWith<net.sergeych.lyng.ScriptError> {
eval("""
fun acceptInts<T: Int>(xs: List<T>) { }
acceptInts([1, "a"])
""".trimIndent())
}
}
@Test
fun testUnioTypeLists() = runTest {
eval("""
fun f<T>(list: List<T>) {
println(list)
println(T)
}
f([1, "two", true])
f([1,2,3])
""")
}
@Test
fun multipleReceivers() = runTest {
eval("""
class R1(shared,r1="r1")
class R2(shared,r2="r2")
R1("s").apply {
assertEquals("r1", r1)
assertEquals("s", shared)
R2("t").apply {
assertEquals("r2", r2)
assertEquals("t", shared)
assertEquals("r1", this@R1.r1)
// actually we have now this of union type R1 & R2!
}
}
""")
}
}