12

Are there any techniques for emulating traits or mixins in Objective-C?

In Scala, for example, I can do something like this:

trait ControllerWithData {
  def loadData = ...
  def reloadData = ...
  def elementAtIndex = ...
}

trait ControllerWithStandardToolbar {
  def buildToolbar = ...
  def showToolbar = ...
  def hideToolbar = ...
}

class MyTableController extends ControllerWithData 
                        with ControllerWithStandardToolbar {
  def loadView = {
     super.loadView

     loadData
     buildBar
  }
}

It's basically a way to combine (or mix in) multiple pieces of functionality into a single class. So right now I have kind of an all-purpose UIViewController that all of my controllers subclass from, but it would be neater if I could break that down and have specific controllers inherit specific behavior.

Bill
  • 44,502
  • 24
  • 122
  • 213
  • Care to explain what traits & mixins are? The concepts may be supported, but known by a different name. – Sherm Pendley Apr 08 '11 at 21:50
  • Looks like someone has implemented Obj-C traits here: http://etoileos.com//news/archive/2011/07/12/1427/ – Bill Jul 12 '11 at 19:08

3 Answers3

18

There's no direct language support, but you could accomplish something similar with message forwarding. Let's say you have trait classes "Foo" and "Bar", which define methods "-doFoo" and "-doBar", respectively. You could define your class to have traits, like this:

@interface MyClassWithTraits : NSObject {
    NSMutableArray *traits;
}
@property (retain) NSMutableArray* traits;

-(void) addTrait:(NSObject*)traitObject;
@end

@implementation MyClassWithTraits
@synthesize traits;

-(id)init {
    if (self = [super init]) {
        self.traits = [NSMutableArray array];
    }
    return self;
}

-(void) addTrait:(NSObject*)traitObject {
    [self.traits addObject:traitObject];
}

/*  Here's the meat - we can use message forwarding to re-send any messages
    that are unknown to MyClassWithTraits, if one of its trait objects does
    respond to it.
*/
-(NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector {
    // If this is a selector we handle ourself, let super handle this
    if ([self respondsToSelector:aSelector])
        return [super methodSignatureForSelector:aSelector];

    // Look for a trait that handles it
    else
        for (NSObject *trait in self.traits)
            if ([trait respondsToSelector:aSelector])
                return [trait methodSignatureForSelector:aSelector];

    // Nothing was found
    return nil;
}

-(void) forwardInvocation:(NSInvocation*)anInvocation {
    for (NSObject *trait in self.traits) {
        if ([trait respondsToSelector:[anInvocation selector]]) {
            [anInvocation invokeWithTarget:trait];
            return;
        }
    }

    // Nothing was found, so throw an exception
    [self doesNotRecognizeSelector:[anInvocation selector]];
}
@end

Now, you can create instances of MyClassWithTraits, and add whatever "trait" objects you'd like:

MyClassWithTraits *widget = [[MyClassWithTraits alloc] init];
[widget addTrait:[[[Foo alloc] init] autorelease]];
[widget addTrait:[[[Bar alloc] init] autorelease]];

You could make these calls to -addTrait: in MyClassWithTraits' -init method, if you want every instance of that class to have the same kind of traits. Or, you could do it like I've done here, which allows you to assign a different set of traits to each instance.

And then you can call -doFoo and -doBar as if they were implemented by widget, even though the messages are being forwarded to one of its trait objects:

[widget doFoo];
[widget doBar];

(Edit: Added error handling.)

Sherm Pendley
  • 13,556
  • 3
  • 45
  • 57
  • Awesome! when you saw the question, you didnt know what traits were and you searchd and even implemented this!!! I wanted to know one thing, why is methodSignatureForSelector needed to be overridden. isnt forwardInvocation enough ? when is methodSignatureForSelector called ? whats the flow? – Amogh Talpallikar Aug 21 '13 at 18:56
0

Traits or Mixins are not supported by Objective-C, you only have built-in option of Categories. But fortunately Objective-C Runtime has almost all tools for implementing own idea if mixing or traits with adding methods and properties to your class at runtime. You can read more about opportunities which Objective-C Runtime provides for you on Apple's documentation website Objective-C Runtime Docs

The idea is:

1) You can create an Objective-C protocol (Mixin), in which you will declare properties and methods.

2) Then you create a class (Mixin implementation), which will implement methods from this protocol.

3) You make your some class, in which you want to provide the possibility of composition with mixins, to conform that protocol (Mixin).

4) When your application launches, you add with Objective-C runtime all implementations from (Mixin implementation) class and properties declared in (Mixin) into your class.

5) voilà :)

Or you can use some ready open source projects such as "Alchemiq"

-4

You're probably looking for categories. See http://macdevelopertips.com/objective-c/objective-c-categories.html.

Zr40
  • 1,910
  • 18
  • 20
  • 1
    Unfortunately, I don't think so. I've considered that and I think if there's some way to do it, it will involve categories. But categories let you mix methods into a specific class, not multiple classes (right?), so I don't think they're enough. But I could be wrong. – Bill Apr 08 '11 at 22:11
  • Categories are indeed defined on a single class. However, other classes inheriting from that class also inherit the categories. – Zr40 Apr 08 '11 at 22:17
  • 4
    Categories are most definitely not traits in the Scala sense. They are more like partial classes in C# land. – Marc W Apr 08 '11 at 23:08