added T is nullable support

This commit is contained in:
sergeych committed 2026-03-14 20:57:15 +03:00
1 parent c54d947f1c
commit 37d093817e
9 files changed
+171 -11

No files matched your search

@@ -535,6 +535,49 @@ class TypesTest {
""".trimIndent())
}
@Test
fun testGenericNullableTypePredicate() = runTest {
eval("""
fun isTypeNullable<T>(x: T): Bool = T is nullable
type MaybeInt = Int?
assert(isTypeNullable<Int?>(null))
assert(!isTypeNullable<Int>(1))
assert(MaybeInt is nullable)
assert(!(Int is nullable))
""".trimIndent())
}
@Test
fun testWhenNullableTypeCase() = runTest {
eval("""
fun describe<T>(x: T): String = when(T) {
nullable -> "nullable"
else -> "non-null"
}
fun describeIs<T>(x: T): String = when(T) {
is nullable -> "nullable"
else -> "non-null"
}
assertEquals("nullable", describe<Int?>(null))
assertEquals("non-null", describe<Int>(1))
assertEquals("nullable", describeIs<Int?>(null))
assertEquals("non-null", describeIs<Int>(1))
""".trimIndent())
}
// @Test
// fun testNullableGenericTypes() = runTest {
// eval("""
// fun t<T>(): String =
// when(T) {
// is Object -> "%s is Object"(T::class.name)
// else -> throw "It should not happen"
// }
// assert( Int is Object)
// assertEquals( t<Int>(), "Class is Object")
// """.trimIndent())
// }
@Test fun testIndexer() = runTest {
eval("""
class Greeter {
@@ -201,6 +201,28 @@ class WebsiteSamplesTest {
assertEquals(12L, (result as ObjInt).value)
}
@Test
fun testNullableTypePredicate() = runTest {
val scope = Script.newScope()
val result = scope.eval(
"""
// Type nullability checks
fun describe<T>(x: T): String = when(T) {
nullable -> "nullable"
else -> "non-null"
}
type MaybeInt = Int?
[describe<Int?>(null), describe<Int>(1), Int is nullable, MaybeInt is nullable]
""".trimIndent()
)
assertTrue(result is ObjList)
val list = result.list
assertEquals("nullable", (list[0] as ObjString).value)
assertEquals("non-null", (list[1] as ObjString).value)
assertEquals(false, (list[2] as ObjBool).value)
assertEquals(true, (list[3] as ObjBool).value)
}
@Test
fun testEasyOperatorOverloading() = runTest {
val scope = Script.newScope()