Test and document supercalls using the same method/property name (#943)

This commit is contained in:
Josh B
2025-02-12 21:10:37 -08:00
committed by GitHub
parent b526902bf0
commit f56b1bb84f
9 changed files with 44 additions and 0 deletions

View File

@@ -2036,6 +2036,29 @@ greeting2 = greetPigeon(parrot) // <4>
<3> Call instance method on `pigeon`.
<4> Call module method (on `this`).
Like other object-oriented languages, methods defined on extended classes and modules may be overridden.
The parent type's method may be called via the `super` keyword.
[source%parsed,{pkl}]
----
open class Bird {
name: String
function canEat(food: String): Boolean = food == "seeds"
}
class InsectivorousBird extends Bird {
function canEat(food: String): Boolean = super.canEat(food) || food == "insects" // <1>
}
swallow = new InsectivorousBird { name = "Swallow" }
canEatSeeds = swallow.canEat("seeds") // <2>
canEatMeat = swallow.canEat("insects") // <3>
----
<1> Call parent class instance method (on `this`).
<2> result: `true`
<3> result: `true`
NOTE: Methods do not support named parameters or default parameter values.
The xref:blog:ROOT:class-as-a-function.adoc[Class-as-a-function] pattern may be a suitable replacement.

View File

@@ -1,18 +1,24 @@
open class One {
prefix = ""
function say(msg) = prefix + msg
function sameMethod() = 1
sameProp = "a"
}
open class Two extends One {
pigeon = super.say("Pigeon")
function barnOwl() = super.say("Barn Owl")
function say2(msg) = super.say(msg) + super.say(msg)
function sameMethod() = 2 + super.sameMethod()
sameProp = super.sameProp + "b"
}
class Three extends Two {
pigeonBarnOwl = super.pigeon + super.barnOwl()
function barnOwlPigeon() = super.barnOwl() + super.pigeon
parrot = super.say("Parrot")
function sameMethod() = 3 + super.sameMethod()
sameProp = super.sameProp + "c"
}
local two = new Two {}
@@ -36,3 +42,5 @@ res9 = two2.say2("Parrot")
res10 = three2.pigeonBarnOwl
res11 = three2.barnOwlPigeon()
res12 = three2.parrot
res13 = three2.sameMethod()
res14 = three2.sameProp

View File

@@ -2,3 +2,5 @@ open module supercalls1
prefix = ""
function say(msg) = prefix + msg
function sameMethod() = 1
sameProp = "a"

View File

@@ -5,3 +5,5 @@ extends "supercalls1.pkl"
hidden pigeon = super.say("Pigeon")
function barnOwl() = super.say("Barn Owl")
function say2(msg) = super.say(msg) + super.say(msg)
function sameMethod() = 2 + super.sameMethod()
sameProp = super.sameProp + "b"

View File

@@ -8,6 +8,8 @@ hidden pigeonBarnOwl = super.pigeon + super.barnOwl()
function barnOwlPigeon() = super.barnOwl() + super.pigeon
hidden parrot = super.say("Parrot")
function other() = super.other()
function sameMethod() = 3 + super.sameMethod()
sameProp = super.sameProp + "c"
prefix = "Oh "
@@ -18,3 +20,4 @@ res4 = pigeonBarnOwl
res5 = barnOwlPigeon()
res6 = parrot
res7 = test.catch(() -> other())
res8 = sameMethod()

View File

@@ -10,3 +10,5 @@ res9 = "Oh ParrotOh Parrot"
res10 = "Oh PigeonOh Barn Owl"
res11 = "Oh Barn OwlOh Pigeon"
res12 = "Oh Parrot"
res13 = 6
res14 = "abc"

View File

@@ -1 +1,2 @@
prefix = ""
sameProp = "a"

View File

@@ -1 +1,2 @@
prefix = ""
sameProp = "ab"

View File

@@ -1,4 +1,5 @@
prefix = "Oh "
sameProp = "abc"
res1 = "Oh Pigeon"
res2 = "Oh Barn Owl"
res3 = "Oh ParrotOh Parrot"
@@ -6,3 +7,4 @@ res4 = "Oh PigeonOh Barn Owl"
res5 = "Oh Barn OwlOh Pigeon"
res6 = "Oh Parrot"
res7 = "Cannot find method `other` in module `supercalls2`."
res8 = 6