Improved instance method resolution logic and added test for Decimal overloading.

This commit is contained in:
Sergey Chernov 2026-03-30 08:53:07 +03:00
parent 286ec30422
commit cdd48ec871
2 changed files with 18 additions and 2 deletions

View File

@ -1180,8 +1180,11 @@ open class ObjClass(
scope: Scope, name: String, args: Arguments,
onNotFoundResult: (suspend () -> Obj?)?
): Obj {
getInstanceMemberOrNull(name)?.let { rec ->
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
classScope?.objects?.get(name)?.let { rec ->
val decl = rec.declaringClass ?: this
if (!rec.visibility.isPublic) {
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
}
if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: scope.raiseError("Internal error: delegated member $name has no delegate")
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()

View File

@ -295,4 +295,17 @@ class DecimalModuleTest {
assertEquals(2, ex.pos.line)
}
@Test
fun testOverloading() = runTest {
eval("""
import lyng.decimal
assert( 1.d is Decimal )
assertEquals(Decimal, abs(1.d)::class)
// here the inference must work:
val t = abs(-10.d)
assert(t is Decimal)
assertEquals(10,t)
""".trimIndent())
}
}