Re: [swift-users] Instantiate Swift class from string

2015-12-11 Thread Matthew Davies via swift-users
The problem is still, how would I call the controller method? I still can't instantiate a new instance of the controller and call a given method on the instance. I was able to partially get this working, but I realized that I still can't instantiate a new controller on each request. I can't figure

Re: [swift-users] Instantiate Swift class from string

2015-12-11 Thread Jeremy Pereira via swift-users
> On 10 Dec 2015, at 20:22, Matthew Davies via swift-users > wrote: > > I'm building a URL router in which I'd like to pass a controller and a > method. I don't want to instantiate all the controllers up front and pass the > methods in as closures, nor do I want old controller instances still

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
Okay I'll have to dig more into using that sort of syntax. And no problem ;) *Matthew Davies* Junior Developer, GeoStrategies Director of Photography, OffBlock Films 209-225-3246 <209-225.3246> | 209-202-3284 | daviesg...@gmail.com | daviesge

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Brent Royal-Gordon via swift-users
(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 “prov

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
You're right. I should be using a class instead… *Matthew Davies* Junior Developer, GeoStrategies Director of Photography, OffBlock Films 209-225-3246 <209-225.3246> | 209-202-3284 | daviesg...@gmail.com | daviesgeek.com

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Daniel Dunbar via swift-users
> On Dec 10, 2015, at 1:01 PM, Matthew Davies via swift-users > wrote: > > Yes I have the protoco​l​, but the problem is that I would want to be able to > call a method on the class that isn't necessarily defined in the protocol. You should only call methods that are defined in the protocol,

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
I could do that, and that would work, but I figured out a way of doing this. import Foundation protocol Controller { init() } class Main : Controller { required init() {} func index() -> String { return "INDEX" } } let inst = Main() let method = Main.index method(inst) func run(ctrl: Controlle

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
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? On Thu, Dec 10, 2015 at 13:52 Brent Royal-Gordon wrote: > For URL routing, I would currently suggest generating code at compile > time. Someday you might be able to replace thi

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Brent Royal-Gordon via swift-users
For URL routing, I would currently suggest generating code at compile time. Someday you might be able to replace this with a macro-based DSL, but for now, I don't see a better option. On the bright side, this does mean you'll have actual machine code doing your routing, which might be faster t

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
Yes I have the protoco ​l​ , but the problem is that I would want to be able to call a method on the class that isn't necessarily defined in the protocol. I.e., I would like to be able to do something like this ​:​ ​---​ protocol Controller { init() } class MainController : Controller {

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
I'm building a URL router in which I'd like to pass a controller and a method. I don't want to instantiate all the controllers up front and pass the methods in as closures, nor do I want old controller instances still kept around. If there's a better way, I'm definitely open to any suggestions. I'm

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Dan Stenmark via swift-users
NSSelectorFromString() is still available in Swift, and you should be able to use the result of that in performSelector, though I’m hesitant to support this approach as it flies in the face of the safety Swift tries to enforce. I’m curious about your use case here; are you trying to create some

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
Ooh okay. I think that should work for my purposes. Thanks. Somewhat related to this, how would I then call a method dynamically on an instance of the class, after instantiating it? --- class Graph { func call(method: String) { // Something goes here } func redraw() -> String { ret

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Daniel Dunbar via swift-users
Note that you can define a protocol which will allow your framework to instantiate the type, and to call methods on instances of that type. If you can structure your code in this fashion, it can be very elegant in that it doesn't require factory functions and it is type safe. For example: -- s

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Matthew Davies via swift-users
I don't really like the idea of a factory function, but unfortunately that might be the only way to do it :( However, due to my specific use case, I don't think a factory function will work. I'm working on a framework that will need to both instantiate the class from a string (or class type) *and*

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Jan Neumüller via swift-users
Please no factory madness in Swift. This stuff is bad enough in Java - don’t infect Swift with it. Jan > On 10.12.2015, at 18:23, Jens Alfke via swift-users > wrote: > > >> On Dec 10, 2015, at 7:26 AM, Harlan Haskins via swift-users >> mailto:swift-users@swift.org>> wrote: >> >> IIRC this

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Jens Alfke via swift-users
> On Dec 10, 2015, at 7:26 AM, Harlan Haskins via swift-users > wrote: > > IIRC this isn’t possible because there’s no Runtime to query for classnames > (it’s inherently unsafe anyway). It’s not unsafe if you specify a base class/protocol that the loaded class must conform to. > You might w

Re: [swift-users] Instantiate Swift class from string

2015-12-10 Thread Harlan Haskins via swift-users
IIRC this isn’t possible because there’s no Runtime to query for classnames (it’s inherently unsafe anyway). You might want to look into a better way of doing that you’re trying to do. — Harlan > On Dec 10, 2015, at 12:42 AM, Matthew Davies via swift-users > wrote: > > I am using the Swift b