From f56b1bb84fc92eddb3ac248bcb2a42e9ca59e6c9 Mon Sep 17 00:00:00 2001 From: Josh B <421772+HT154@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:10:37 -0800 Subject: [PATCH] Test and document supercalls using the same method/property name (#943) --- .../language-reference/pages/index.adoc | 23 +++++++++++++++++++ .../input/classes/supercalls.pkl | 8 +++++++ .../input/modules/supercalls1.pkl | 2 ++ .../input/modules/supercalls2.pkl | 2 ++ .../input/modules/supercalls3.pkl | 3 +++ .../output/classes/supercalls.pcf | 2 ++ .../output/modules/supercalls1.pcf | 1 + .../output/modules/supercalls2.pcf | 1 + .../output/modules/supercalls3.pcf | 2 ++ 9 files changed, 44 insertions(+) diff --git a/docs/modules/language-reference/pages/index.adoc b/docs/modules/language-reference/pages/index.adoc index 453434dd..525dd090 100644 --- a/docs/modules/language-reference/pages/index.adoc +++ b/docs/modules/language-reference/pages/index.adoc @@ -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. diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/classes/supercalls.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/classes/supercalls.pkl index c889ece3..7b955f71 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/classes/supercalls.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/classes/supercalls.pkl @@ -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 diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls1.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls1.pkl index e503e840..2fdecb26 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls1.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls1.pkl @@ -2,3 +2,5 @@ open module supercalls1 prefix = "" function say(msg) = prefix + msg +function sameMethod() = 1 +sameProp = "a" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls2.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls2.pkl index 1dfe9015..1706f759 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls2.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls2.pkl @@ -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" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls3.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls3.pkl index e1eb313a..30a3c130 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls3.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/supercalls3.pkl @@ -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() diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/classes/supercalls.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/classes/supercalls.pcf index 2773319e..9baffe63 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/classes/supercalls.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/classes/supercalls.pcf @@ -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" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls1.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls1.pcf index 6691f56a..5cd4fa0e 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls1.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls1.pcf @@ -1 +1,2 @@ prefix = "" +sameProp = "a" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls2.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls2.pcf index 6691f56a..3028e4f8 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls2.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls2.pcf @@ -1 +1,2 @@ prefix = "" +sameProp = "ab" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls3.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls3.pcf index 7f178399..0380e42f 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls3.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/supercalls3.pcf @@ -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