I have an object (Bar
) like this that shadows the implementation of the function foo
depending on T
:
struct Bar {
func foo() {
print("foo generic")
}
func foo() where T == String {
print("foo string")
}
}
When I call it it works like I expect it to:
B().foo() // foo generic
B().foo() // foo string
Now I create an another function that calls foo
inside the object:
func boo() { foo() }
But when I call boo
, it stopped using the string “shadow” altogether.
B().boo() // foo generic
Things I tried:
- I tried to mark the generic function
@_disfavoredOverload
but it didn’t do anything. - I tried exporting the
foo
function to another object. And I was surprised when this didn’t work. I’d like to know why this solution didn’t work.
struct Bar {
let inner: Far
func boo() {
inner.foo()
}
}
struct Far {
func foo() {
print("foo generic")
}
func foo() where T == String {
print("foo string")
}
}
Notice:
I ran into this issue while creating a SwiftUI body. So I’m unable to use some solutions.
// Very simplified.
struct MyView: View {
let client: APIClient
var body: some View {
Button("Fetch") {
Task(operation: fetchData) // Always calls generic
}
}
func fetchData() async {
client.fetch()
}
func fetchData() async where APIClient: FetchSpecially {
client.specialFetch()
}
}