[swift-evolution] Java-style annotations (attributes?)

Rick Mann rmann at latencyzero.com
Tue Sep 27 19:02:03 CDT 2016


I did a little googling, but didn't find this specific question being asked. I've had the question in my head for a while now, so I apologize if I already brought this up.

Will Swift ever support introspectable Java-style annotations? I think the current syntactic construct is called an Attribute, but they'd be customizable, created in much the same way as classes are.

They provide a very convenient way to provide plug-ins to a containing environment. For example, web servers are often built in Java using these techniques. You define a (plain) class that represents a service, whose methods represent individual request endpoints:

@Service(root="/my/sub/webapp/")
class
MyWebapp
{
    @Get(path="users/{id}/")
    func getUsers(@Param("id") userID inUserID: Int)
	-> [String:AnyObject]?
    {
	//  return dictionary for user with ID inUserID
    }
}

The container uses introspection to find all classes annotated with "Service." It pulls the values of the parameters out of the Service annotation using introspection. It then does a similar thing on the methods in the Service class, etc.

Is there any work being done along these lines?


-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-evolution mailing list