Can Objective-C categories serve the same purpose as Traits do in Scala?
-
The statement of the question seems little abstract to me, so please read the details below :) Since the time when C++ was the first choice Object Oriented Language for almost anything,We had a big problem of Multiple inheritance in C++ when two different implementations of same function declared or defined in a (abstract or concrete)parent class of the two, come to a common child class and create ambiguity, popularly known as the "Diamond" problem. C++ gave a solution : the "virtual" keyword, to be used while specifying the parent class(es) so that only one copy goes to the final child class being decided at runtime instead of compile time based on certain rules. Java tried to solve this by providing Single Inheritance and avoiding Multiple Inheritance completely. But then it lacked a feature where a class behaviour could be influenced by a bit and that may change in future or more behaviour may be added to it and we needed a way to encapsulate behaviour, then came the construct "interface" which had nothing but method declarations which each implementing class had to implement in their own way. protocols in Objective C are more or less the same thing. However this approach has a major drawback, and this is where my question comes into picture. The drawback is, if a class has many subclasses implementing the same interface in the same way. The same code has to be written everywhere in each class. If it was all the classes, then the implementation could have been there in the parent class which gives the implementation. At max what can be done is to introduce a class with the implementation of the common method in between the current parent class and make all those classes inherit from it. But this can lead to lot of bugs if not done properly in a complex project. Scala provides Traits which not only serve as interfaces but can add full blown implementations for few of them that can be mixed with some classes that require it and removed when not. I haven't seen the syntax but I have heard that Ruby solves the same problem using something called as mixins. In case of Objective C, We have protocols but they have the same limitations as Java interfaces. So my question is, If I create a category of a class and import it in any subclass of that class, Will the subclass get all those implementation methods ? Also if I remove it, will it be synonymous to removing the trait inclusion declaration for a class in Scala? I might be wrong in any of my assumptions above, I am newbie when it comes to OOP design (and just got interested in FP ). I would also appreciate any better solutions than this one.
-
Answer:
It is not an out-of-the-box feature of Objective-C, but because you have access to low level runtime details you can actually implement Mixin-like behavior by, at runtime, adding method implementations to a class. In fact, https://github.com/jspahrsummers/libextobjc has a primitive implementation of it if you want to take a look at that.
Ian Ynda-Hummel at Quora Visit the source
Related Q & A:
- How do I call an objective-c method?Best solution by Stack Overflow
- Where can I find restaurants in Manila that serve halal food?Best solution by annalyn.net
- Can somebody suggest an easy to use DVD recorder for the purpose of recording from camcorder onto a DVD?Best solution by imshopping.com
- Where best can one learn c,c++ from?
- Is there a site where I can learn C++ easily?Best solution by Blurtit.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.