Last active
December 17, 2015 17:09
-
-
Save iamleeg/5644043 to your computer and use it in GitHub Desktop.
Thoughts on how prototypical inheritance might be done in Objective-C. Notice that the Étoilé runtime (the predecessor to the GNUstep runtime) actually supports such things anyway.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
@interface A : NSObject | |
- (void)createPrivateSubclass; | |
@end | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool | |
{ | |
A *a = [A new]; | |
NSLog(@"Before: %@", [a class]); | |
[a createPrivateSubclass]; | |
NSLog(@"After: %@", [a class]); | |
} | |
} | |
@implementation A | |
- (void)createPrivateSubclass | |
{ | |
NSString *className = [[NSUUID UUID] UUIDString]; | |
Class newClass = objc_allocateClassPair([self class], [className UTF8String], 0); | |
object_setClass(self, newClass); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is an interesting step towards prototypal inheritance, definitely; actual cloning is of course more complex as methods have to be resolved with respect to the parent. Interesting.