Created
September 24, 2021 19:10
-
-
Save manicaesar/984ea25673b5dc412ce3fb05ea4deaa2 to your computer and use it in GitHub Desktop.
Swift property overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StructWithTwoObjects { | |
let object: String | |
/* | |
var object: A { // Stupid, but causes compilation error - OK! | |
return self | |
} | |
*/ | |
} | |
// --------------------- | |
protocol SomeProtocol {} | |
protocol ObjectProviding { | |
associatedtype ObjectType: SomeProtocol | |
var object: ObjectType { get } | |
} | |
extension ObjectProviding where | |
Self: SomeProtocol, // It is not needed, can be commented out | |
Self.ObjectType == Self { | |
var object: ObjectType { | |
return self | |
} | |
} | |
struct ObjectContainer: SomeProtocol, ObjectProviding { | |
let object: String | |
init(object: String) { | |
self.object = object | |
} | |
} | |
struct ObjectPrinter<T: ObjectProviding> { | |
let objectContainer: T | |
init(objectContainer: T) { | |
self.objectContainer = objectContainer | |
} | |
func printTypeOfContainedObject() { | |
return print(type(of: objectContainer.object)) | |
} | |
} | |
let objectContainer = ObjectContainer(object: "string") | |
let printer = ObjectPrinter<ObjectContainer>(objectContainer: objectContainer) | |
print(type(of: objectContainer.object)) // Prints: String | |
printer.printTypeOfContainedObject() // Prints: ObjectContainer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
O wow! That's a super interesting thread, glad you found it ❤️
While it's (a little) reassuring that this is expected and well-defined behavior, I don't think I personally like the potential implications of all of this, especially when we move this discussion to consider cross-framework implications.
Since in your merged sample we see that we can force different
value
to be used by giving some hints to the compiler than this becomes possible :This can be unexpected from the
External Module
consumer perspective - the compiler obviously told them that they need to implementfunc fvalue()
to fulfil the requirements but in no other way hinted them that there are other requirements that have default values - so they aren't aware that they "silently" have anInt
property and would (rightfully so!) expect this to fail, or maybe assume that there is a conversion happening under the hood (conversion fromInt
toString
would be more natural, but still)Not to mention the samples that Xiaodi Wu and Marc Palmer mentioned in their posts 🤯
It can easily lead to a whole lot of errors that may be confusing to track down.