Skip to content

Instantly share code, notes, and snippets.

@telephon
Created May 25, 2024 12:51
Show Gist options
  • Save telephon/0e31961dfd1599ddc3ce4e158e6e3dcb to your computer and use it in GitHub Desktop.
Save telephon/0e31961dfd1599ddc3ce4e158e6e3dcb to your computer and use it in GitHub Desktop.
delegation test for doesNotUnderstand / doesNotUnderstandAbout
SuperKlasse {
// this is the interface in Object
// if an object does not understand,
// we look which of the two the class implements,
// and call the appropriate one.
doesNotUnderstand { |selector ... args|
"I am the super class, doesNotUnderstand. I have received the following arguments:\n%\n%\n".format(selector, args).postln;
"I'll forward to doesNotUnderstandAbout".postln;
^this.doesNotUnderstandAbout(selector, args)
}
doesNotUnderstandAbout { |selector, args, keywordArgumentPairs|
^"I am the super class, doesNotUnderstandAbout. I have received the following arguments::\nselector:%\nargs:%\nkeywordPairs:%\n"
.format(selector, args, keywordArgumentPairs)
}
}
SubKlasse : SuperKlasse {
// this one implements the new interface.
doesNotUnderstandAbout { |selector, args, keywordArgumentPairs|
^"I have correctly received the following arguments:\nselector:%\nargs:%\nkeywordPairs:%\n".format(selector, args, keywordArgumentPairs)
}
}
SubSubKlasse : SubKlasse {
// let's suppose, the subclass is older than the interface change, and still forwards to doesNotUnderstand
// when it receives a call that it doesn't understand, it still calls this one.
doesNotUnderstand { |selector ... args |
// do something, and in certain cases, delegate up:
// this would be wrong, because the superclass implements |selector, args, keywordArgumentPairs|
^super.doesNotUnderstand(selector, *args)
}
}
/*
a = SubSubKlasse.new;
a.plink(1, 2, 3);
a.doesNotUnderstand(\plink, 1, 2, 3);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment