[swift-users] Instantiate Swift class from string
Matthew Davies
daviesgeek at gmail.com
Thu Dec 10 18:13:49 CST 2015
Okay I'll have to dig more into using that sort of syntax.
And no problem ;)
*Matthew Davies*
Junior Developer, GeoStrategies <http://geostrategies.com>
Director of Photography, OffBlock Films <http://offblockfilms.com>
209-225-3246 <209-225.3246> | 209-202-3284 | daviesgeek at gmail.com |
daviesgeek.com
<http://facebook.com/daviesgeek> <http://us.linkedin.com/in/daviesgeek>
<http://twitter.com/daviesgeek> <http://daviesgeek.com/feed.xml>
<http://github.com/daviesgeek>
On Thu, Dec 10, 2015 at 4:12 PM, Brent Royal-Gordon <brent at architechies.com>
wrote:
> (Sorry for the repeat, Matthew.)
>
> > Can you clarify a bit on that? So, what you're saying is to generate
> routing code based off the DSL router I've written?
>
> Yes, unless you can modify your DSL so you can directly provide your
> controller classes, you’ll probably need to generate code.
>
> By “provide your controller classes”, I mean that you could make a routes
> file which looked something like this (this routing syntax is loosely
> inspired by Rails):
>
> import MyFramework.Router
>
> // UsersController.self is an instance which represents the
> UsersController class.
> // It’s of type UsersController.Type, which might (for instance)
> be a subtype of WebController.Type, etc.
> Router.root.resources(UsersController.self, path: “users”) { users
> in
> users.resources(PostsController.self, path: “posts”) {
> posts in
> // PostsController.comments is a way to retrieve a
> closure which calls PostsController’s comments() instance method.
> // You use it by saying something like
> `myClosure(myControllerInstance)(arg1, arg2, etc)`.
> posts.get(PostsController.comments, path:
> “comments”)
> }
> users.resources(CommentsController.self, path: “comments”)
> }
>
> Note that in all cases you pass instances, not names, to your routing
> APIs. This allows you to write code that can work with any compatible class
> or method without any danger of trying to use a class or method that
> doesn’t exist.
>
> But using this approach limits the flexibility of your routing DSL’s
> design. For instance, you can’t just take the string “users” and infer that
> you should use UsersController. If you want that sort of more sophisticated
> behavior, you’re going to need to generate Swift code instead.
>
> --
> Brent Royal-Gordon
> Architechies
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151210/e6ecdf4f/attachment.html>
More information about the swift-users
mailing list