custom attribute in userland (annotation).


(Axel Etcheverry) #1

Hello Swift Community,

This is my first proposal...

What do you think to add custom attributes in Swift?

Example of use: https://gist.github.com/euskadi31/2e15d21bc718b6f02f3497ad3e0a531d

For recovery of the attributes in the runtime, we could go through reflection or in the compilation phase and accessible via a property or method.

···

--
Best regards,
Axel Etcheverry
CTO at Kokoroe

Phone : +33(6) 84 15 57 52
Website : www.kokoroe.co <https://www.kokoroe.co/>


(Brent Royal-Gordon) #2

What do you think to add custom attributes in Swift?

Example of use: https://gist.github.com/euskadi31/2e15d21bc718b6f02f3497ad3e0a531d

For recovery of the attributes in the runtime, we could go through reflection or in the compilation phase and accessible via a property or method.

Swift's current reflection APIs are massively underpowered to support this feature. For instance, there is no way to list or retrieve types, methods, initializers, subscripts, or most of the other things you would want to attach attributes to. Even property access is extremely limited—you can only list the names and values of all properties on a specific instance. Designing this would have to come before the sort of custom attributes you want, because there's simply no way to access the attributes without them.

You may be interested in the property behaviors proposal: <https://github.com/apple/swift-evolution/blob/master/proposals/0030-property-behavior-decls.md> This would introduce a broadly similar feature for properties, though it would work in a different way: Rather than annotating entities with information you could discover through a reflection API, property behaviors would install custom getters and setters which could call into functionality provided by the instance, use storage private to the behavior, and offer per-property customization hooks along the lines of `willSet` and `didSet`.

Unfortunately, the proposal was sent back for revision, and at this point the team's focus is shifting towards implementing already-approved changes and polishing existing features as we get closer to release. I'm just a guy, not a member of the core team, but I'm guessing nothing in this space will be seriously considered before planning for Swift 4 begins.

···

--
Brent Royal-Gordon
Architechies