Here’s a great use case for Parameter Packs 🤌

Hi 👋

Before we go into the topic of this email, I have a big thank you to my sponsor this week: Proxyman 🧑‍🚀


Advertisement

Need to debug HTTP/HTTPS network on iOS apps?

Try Proxyman, a native macOS app that captures and displays Request/Response in beautiful UIs.

Supports iOS device and Simulator.

👉 Download macOS app now! 👈


Sponsors like Proxyman really help me grow my content creation, so if you have time please make sure to check out this product: it’s a direct support to my content creation ☺️


If you’ve been following the recent release of Swift 5.9, you might have heard about a new feature called Parameter Packs.

Parameter Packs have introduced the possibility of dealing with several generic arguments at once.

(and it’s actually thanks to them that SwiftUI views are no longer limited to having 10 child views at maximum!)

If you want to learn about Parameter Packs in details, you can check out the replay of my livestream from last Thursday, where I breakdown this complex feature step by step:

I concluded this livestream by sharing a very nice real world use case for Parameter Packs, and that’s what I want to share with you in this email!

If you use SwiftUI, you’re probably aware of how its Environment is a very convenient way to inject values into the view hierarchy.

And it’s possible that you might have been using the Environment to inject closures that your view will call in reaction to user interaction:

This approach is very convenient, but has a small drawback: since the protocol EnvironmentKey expects a defaultValue, we’ve had to make the handler optional.

It’s convenient, but it also means that if we forget to inject the closure into the view hierarchy, our app will be non-responsive:

A good way to solve this issue is to stop using nil as a defaultValue, and instead provide a closure that will trigger an assertionFailure(): this way if we forget to inject the closure, the mistake will be easier to spot:

(in addition to using assertionFailure(), you might also want to log an error to a monitoring service, so that you can be alerted if the issue happens in production!)

All we need to do for this to work it to call makeDefaultHandler() to provide the defaultValue:

But the thing is, the function makeDefaultHandler() only works when the closure expects no arguments.

And as you can imagine, that will not always be the case: soon we’ll need to deal with closure that take one argument (a selected value, for instance), then two, then three, and so on…

But the good news is that thanks to Parameter Packs we no longer need to declare one overload for each number of arguments we want to handle.

Instead, we are now able to write a single implementation…

…that will work with any number of expected arguments in the closure 👌

That’s all for this email, I hope you’ve enjoyed this nice real world example of how Parameter Packs can be put to good use!

Of course, if this is the first time you’re hearing about Parameter Packs, you might have a lot of question regarding the code just above 😅

So if you’re in that situation, I really recommend you watch the replay of my livestream: it should teach you everything you need about this new feature of Swift 5.9 😌

That’s all for this email, thanks for reading it!

If you’ve enjoyed it, feel free to forward it
to your friends and colleagues 🙌

I wish you an amazing week!

❤️

Previous
Previous

Hidden feature: enum

Next
Next

This small mistake will crash your SwiftUI app 🧨