|
Could you please help me with a hint for one question? I don't understand why one code does work ok, and another similar doesn't. This code works ok:
s3 is closure, where I can pass UILabel. And func + can accept two closures, containing different types - UIView and UILabel. But following code gives an error:
This code gives following compile error:
I more or less understand why second code gives an error. But I can't understand why first code gives no errors. Do you have any idea? If you know some guides, docs or articles where I can read about it, I'll really appreciate if you send some links to me. Thanks! |
|
There is some very strange business going on. It could perhaps be a bug. Consider the following modifications: First Test Change
to
The compiler will then not allow the function Second Test Attempt the following: print the types of the closures passed in as parameters from the closure returned by
Notice that
generates a warning that the cast always succeeds (which is indeed true) and that coercion should be used instead. Now here comes the possible bug:
The compiler issues a warning that the I have not run into a situation quite like this so there may be something deeper that I am missing with implicit coercion. There is some reason why the compiler might allow this. Since the If anybody has any other explanations let us know! |
|
Actually I've got an answer at Stackoverflow. It's due to kind of sophisticated computer science layer Swift language features - invariance, covariance and contravariance :). Link to the answer: https://stackoverflow.com/a/62342173/5186503 Copying the answer here:
You are running into the issue of Swift generic coercion misunderstanding. Generic types in Swift are invariant, which means that This is why Because of this, you can pass a UILabel to a
|
|
@n0an Could you explain the result of the |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.