The third goal is that macro expansions should be incorporated into the program in predictable, additive ways. And a macro's implementation can validate its inputs and emit compiler warnings or errors if something's wrong, so it's easier to be certain that you're using a macro correctly. Nor can you pass an argument with the wrong type because macro arguments and results are type-checked, just like function arguments. You can't pass "1 +” to a macro because arguments have to be complete expressions. The second goal is that both the code passed into a macro and the code sent back out of it should be complete and checked for mistakes. And if you don't see #’s or you can be confident that there aren't any macros involved. Swift already uses the pound (#) and at signs to indicate special compiler behavior. And attached macros are used as attributes on declarations in your code. There are two kinds of macros: Freestanding macros stand in place of something else in your code. The first goal is that it should be pretty obvious when you're using a macro. We designed them with four goals in mind. But Swift macros are very different in ways that avoid many of those issues. That's partly because a lot of Swift developers are familiar with Objective-C or other languages that use the C preprocessor, and they know about the limitations and pitfalls of C macros. But if you have, you may have mixed feelings about them. Some of you haven't used macros in other languages. They let you add your own language features to Swift, eliminating tedium and boilerplate, in a way you can distribute in a Swift package without modifying the compiler. But that literally involves me, personally, getting on a video conference and discussing your feature with other Swift project leaders, so it's not exactly a process that scales. But what if the existing features can't do what you want? Well, you could add a feature to the Swift compiler, since it's open source. You write some simple syntax, and the compiler expands it into a more complicated piece of code automatically. Swift has many features that work in this way. Creating this code for you lets you use Codable without having to know exactly how it works and makes it so you don't have to decide if adding Codable support is worth writing a screenful of code. I've shown the expansion for the conformance in the gray box. For example, when you conform to Codable without providing implementations for its members, Swift automatically expands the conformance into a set of members that it inserts into the program. These features all work basically the same way. That's why it provides features like derived conformances and result builders, which help users to avoid writing repetitive boilerplate. Swift loves to let users write expressive code and APIs. So let's get started by talking about why Swift supports macros. And then we'll cover how Swift macros work and the specific ways they can interact with the other code in your project.Īfter that, we'll talk about how to implement macros and finish by discussing how to make sure your macros work correctly. Then, we'll talk about some of the principles we kept in mind when we designed Swift macros. We'll start by talking about what macros are for. Today we'll be talking about Swift macros, an exciting new feature that lets you customize the Swift language for your needs. ♪ ♪ Becca: Hi, I'm Becca from the Swift team.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |