Got an idea how Swift could be minimally augmented to allow user types calling through original implementations of autogenerated methods. Currently it's "all or nothing": if I override the method to make a minute change on top of what standard library is doing ā I can't access the standard library synthesised implementation and have to recreate everything from scratch.
I will use Hashable protocol here as an example, but it's applicable to anything else.
- When I make my type
Hashable
and standard library can synthesise "hash(into:)" method, it's doing this:
struct T: Hashable {
/*autogenerated*/ func auto_synthesized_hash(into hasher: inout Hasher) {
// original implementation
}
/*autogenerated*/ func hash(into hasher: inout Hasher) {
auto_synthesized_hash(into: hasher)
}
}
- if the type overrides hash it could call through the synthesized method:
extension T {
/* user defined "override" */
func hash(into hasher: inout Hasher) {
print("calling synthesized")
auto_synthesized_hash(into: hasher)
hasher.combine(extraField)
}
}
in this case there will be no "autogenerated" func hash
.
-
if the type overrides
hash
and doesn't call synthesized hash method anywhere - the synthesized hash method could be stripped. -
If the type doesn't override
hash
ā linker inlinesauto_synthesized_hash
body intofunc hash
.
A variation of this idea (albeit the one that will require more changes to the language / compiler) is introducing a keyword similar to super
:
/* user defined "override" */
func hash(into hasher: inout Hasher) {
print("calling synthesized")
auto_synthesized.hash(into: hasher)
hasher.combine(extraField)
}
Is this idea worth exploring?