Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind subclass to superclass #7

Closed
Undistraction opened this issue Sep 21, 2011 · 7 comments
Closed

Bind subclass to superclass #7

Undistraction opened this issue Sep 21, 2011 · 7 comments

Comments

@Undistraction
Copy link

Unless I am missing something there is currently no way to bind a class to another class.

This would allow a parameter typed to an Abstract superclass to be satisfied by a Concrete subclass.

The method signature would look like this:

  • (void)bindClass:(id)aClass toClass:(Class)bClass;
@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

It looks like you are trying to bind to an interface of a certain type that has a number of implementations. In your case, a set of subclasses. The same thing could be done using a protocol binding.

[self bindClass:[Myclass class] toProtocol:@protocol(Myprotocol)]

@Undistraction
Copy link
Author

Are you saying an alternative approach would be to use a protocol binding?

Because how can I pass the superclass to the second argument if it is expecting a Protocol?

[self bindClass:[MyConcreteSubclass class] toProtocol: [MyAbstractSuperclass]]; //This generates a warning but no error and does not work

In some cases using creating a protocol would be a good alternative, but I think this would be a very useful addition to the API.

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

I'm arguing that you can bind the subclasses against an protocol that reflects the methods and properties that need to be used.

@Undistraction
Copy link
Author

I suppose that is better practice, however I still think this could result in unnecessary protocols. In a situation where a property will only ever need to accept subclasses of an abstract class, writing a protocol would be redundant.

More importantly, what if we wanted to say; 'When asked for UITableView, satisfy the dependency with SpecialisedTableView'. UITableView has no protocol relating to the majority of its API. So a huge protocol would need to be created in order to expose its API. The same would be true of all UIView subclasses. Surely in this instance being able to bind to UITableView would make a lot of sense?

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

That is a fair point. I wanted to make sure we weren't heading down supporting an anti-pattern. You could probably achieve what you want, for now, using a provider. I'll add this as a feature request.

@Undistraction
Copy link
Author

Thanks a lot.

@Undistraction
Copy link
Author

This is actually really easy to achieve using your current setup. By creating a second method on JSObjectionModule that takes a Class instead of a protocol it is possible to use the exact same mechanics as binding to a protocol, only using the classname instead. It is also necessary to add a check that one class is derived from the other of course, but it seems that your architecture supports this functionality with very minor ammends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants