I have read somewhere that in future swift versions there will be compiler warning when you are using protocol types (ex. in method params, inits, etc.) you should specify keyword any. This warning is a result of dispatch types and it helps helps the developer understand performance degradation. So we can use generics while using protocols. Example: protocol MyProtocol {} 1. func doSomething(message: MyProtocol) {} 2. func doSomething(message: T) where T: MyProtocol {} 3. func doSomething(message: some MyProtocol) {} This looks like the same, but it isn't. 2nd and 3rd do the same thing using static dispatch, unlike 1st, that uses dynamic dispatch. So, the question is, if I'm correct, do you know about it and should we use some keyword to all protocol types even without associated types?
Thank you for the video! The docs say all of the possible return values must have the same type. However I saw a SwiftUI example which returns different types and that gets compiled successfully. struct Navigation: View where Content: View { @ViewBuilder var content: () -> Content var body: some View { if #available(iOS 16, *) { return NavigationStack(root: content) } else { return NavigationView(content: content) } } } Could you explain why that works or point me out to some resources with a good explanation? It's been a week trying to understand it...
Follow for more linkedin.com/in/afrazsiddiqui
Nice! I like the clear explanations and focus on a specific topic in digestible time.
Glad it was helpful!
I have read somewhere that in future swift versions there will be compiler warning when you are using protocol types (ex. in method params, inits, etc.) you should specify keyword any. This warning is a result of dispatch types and it helps helps the developer understand performance degradation.
So we can use generics while using protocols.
Example:
protocol MyProtocol {}
1. func doSomething(message: MyProtocol) {}
2. func doSomething(message: T) where T: MyProtocol {}
3. func doSomething(message: some MyProtocol) {}
This looks like the same, but it isn't. 2nd and 3rd do the same thing using static dispatch, unlike 1st, that uses dynamic dispatch.
So, the question is, if I'm correct, do you know about it and should we use some keyword to all protocol types even without associated types?
Yes indeed
This helped alot! Thanks!
Glad it helped!
Great !! thanks for sharing.
Youre welcome
Thank you for the simple explanation much appreciated. Was banging head across room cause of it, read many times couldn't understand the practical use
Glad it helped!
appreciate 🎉
Thanks
Thank you for the video! The docs say all of the possible return values must have the same type. However I saw a SwiftUI example which returns different types and that gets compiled successfully.
struct Navigation: View where Content: View {
@ViewBuilder var content: () -> Content
var body: some View {
if #available(iOS 16, *) {
return NavigationStack(root: content)
} else {
return NavigationView(content: content)
}
}
}
Could you explain why that works or point me out to some resources with a good explanation? It's been a week trying to understand it...
Oh I guess I’ve found where to dig next. It seems to be the ViewBuilder which does this magic trick
Comparing to Rust, seems like "some" is "impl", and "any" is "dyn"
谢谢,这是一个非常有用的视频。
Youre welcome
Thanks for this video, can you make a video on the problem with using any rather than some
Sure thing!
So the main idea of some is making type erasing easier, isn’t it? But not using diff types in typealias since it will likely brake the execution
Yes
nice
Thanks
there are saint people. you're definitely one of them
Ha thanks