Hello, today I created a small piece of code to try out an idea, but the result was not what I expected. Given this snippet: Generics.swift · GitHub . I expected to get 2x "Hi, I am generic func" and 3x "Hi, I am special func" outputted in the console. Instead I got 4x "generic" and one "special" and this is the part that confuses me. From what I understand, runNormalGeneric
and makeOpaque
cause the compiler to know the underlaying type, which is TestB, and I expected it to call the perform
function declared for it. I would be grateful if someone could explain to me or point me to a link to an explanation of why the compiler chooses the "generic" variant of the method.
The purpose of an opaque result type is to hide the underlying type, so the compiler intentionally doesn’t “know” it outside the function. The compiler always picks the most specific overload to call based on static type information at the call site, which is the generic overload in this case.
1 Like
So I misunderstood how opaque result type behaves, thank you very much for explanation.