mirror of
https://github.com/apple/pkl.git
synced 2026-03-13 21:55:31 +01:00
Test and document supercalls using the same method/property name (#943)
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,3 +2,5 @@ open module supercalls1
|
||||
|
||||
prefix = ""
|
||||
function say(msg) = prefix + msg
|
||||
function sameMethod() = 1
|
||||
sameProp = "a"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
prefix = ""
|
||||
sameProp = "a"
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
prefix = ""
|
||||
sameProp = "ab"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user