Code Generation in Swift — Gain time, type safety and more!
updated 9 months ago; latest suggestion 9 months ago
Swift is a great, type safe and powerful language. But some stuff are still cumbersome to write yourself. And some APIs could deserve more type-safety and less string-based methods.
In this talk we'll discover some popular tools to do code generation for Swift, especially Sourcery and SwiftGen. We'll see how they work, what you can do with them, some concrete and advanced examples of how your code can be made safer, and how you can save a lot of development time thanks to those tools.
What this talk will cover
- What Code-Gen can help you with, and when it can be useful
- An overview of existing Code-Gen tools, including SwiftGen and Sourcery
- An overview of how you can customise the generated code to you needs using Stencil templates
- Concrete examples of using those tools, including generating:
- type-safe constants for resources like images and localizable strings, that can be type-checked and typo-checked at compile time
- implementations for protocols and patterns like Equatable, Lenses, Decorators… and similar implementations that are really cumbersome to write manually
- automatic API parsing code based on metadata and annotations
- stub data and test code
- and other advanced usages
@ f638d4024879aa2cf78b9a27f85e0acd9451e998 Thanks for your suggestion!
Indeed, I'll try to focus on more specific and nontrivial cases if possible, that's a good idea. The risk though is loosing the people that don't know anything about Sourcery nor SwiftGen and never used those tools before if the generic introduction part is too short, but it's probably worth focusing on the details and let people read the basics on their own during breaks as you suggest. Will try to see how I can balance that to give more time to concrete nontrivial examples.
I am not a user of SwiftCheck so I don't think I'll be able to cover that part but generating property-based tests could definitely be covered. As well as Decorators, Lenses, and similar stuff, which are great applications for code gen.
I personally already use liftoff a lot, and it's definitely a thing. But though I think I'll mention it in my talk in the introduction as tools generating stuff for you, I don't think I can dedicate an entire section to it, as again, even talking only about SwiftGen and Sourcery is very hard in the short time window we have (my last try on that topic for a work presentation took about 1h, liftoff included, and my audience already knew the basics of codegen…) so I prefer to focus on advanced usage of SwiftGen and Sourcery instead. Does that make sense to you?
That should be an interesting talk for everyone doing Swift, because code generators can fill some gaps in that language. And it can be really a very broad topic.
While general overview might be necessary (especially for Sourcery being relatively new project) it would be really cool to hear some non-trivial use cases of using these tools, something that you can not find in their README, which attendees can always check out after the conference on in a break. Speaking of Sourcery that can be already mentioned tests, but not just LinuxMain generation, but maybe generating test cases for property based testing, i.e. using SwiftCheck? Generating decorators (or proxies if you wish), which is PITA in Swift? In other words few examples demonstrating something exciting that no one even though that it's even possible.
There are also other code generators (not Swift-only) which are aiming at helping to generate boilerplate project structures rather than single bits of functionality (liftoff and Generamba to name a few). Are they a thing? Would be cool to hear how all of that fits in modern iOS dev pipeline.
Of course it will be impossible to fit all of that in 30 min talk, what makes it even more important, in my opinion, to try to give more "exclusive" content, that tells something new about the topic, not just a general overview.
I might be wrong though =)
@9e3e54f9924a8a85c7af0c128fc9c90cd86b1ed7 it depends what you mean by generating test code but yes that's among the things that's possible, including auto generating the LinuxMain.swift for SPM to be able to know which test methods to run during unit testing.
But in 30 min I don't think I'll have time to go into details about generating while test suites sadly, but rather a more broad and global overview with concrete examples to e.g. generate Equatable implementations automatically or auto generate an API parsing code for your models etc
Are you also planning to cover generating test code? Is that even possible?