OpinionatedC icon indicating copy to clipboard operation
OpinionatedC copied to clipboard

find practical way to allow optional extension method prefixing

Open leoschweizer opened this issue 9 years ago • 6 comments

Since the best practice for category methods clearly is to have a vendor prefix, it would be nice to allow optional prefixing for the OpinionatedC extensions. The mechanism should be easily configurable, and rock solid when activated.

A first idea is implemented (very roughly) on branch spike-optionalprefixes.

leoschweizer avatar Aug 20 '15 08:08 leoschweizer

hello I think this could be useful.

the following project used optional prefixes. https://github.com/matthewcheok/POP-MCAnimate/tree/master/POP%2BMCAnimate/Shorthand

it defines a flag before importing the code. all the properties and methods are prefixed by default.

#define MCANIMATE_SHORTHAND
#import <POP+MCAnimate.h>

internally it defines a special header that wraps the properties

#define MCSHORTHAND_GETTER(getter, ctype) \
- (ctype)getter { \
return [self pop_##getter]; \
}

#define MCSHORTHAND_SETTER(setter, getter, ctype) \
- (void)set ## setter :(ctype)value { \
[self setPop_ ## getter :value]; \
}

#define MCSHORTHAND_PROPERTY(getter, setter, ctype) \
MCSHORTHAND_GETTER (getter, ctype) \
MCSHORTHAND_SETTER (setter, getter, ctype)

then the flag enables the shorthand sintax by using and #ifdef.

#import <UIKit/UIKit.h>
#import "MCShorthand.h"

@interface UIView (MCShorthand)

@property (assign, nonatomic) CGFloat pop_scaleX;
@property (assign, nonatomic) CGFloat pop_scaleY;
@property (assign, nonatomic) CGPoint pop_scaleXY;

@end

#ifdef MCANIMATE_SHORTHAND

@interface UIView (MCShorthand_DropPrefix)

@property (assign, nonatomic) CGFloat scaleX;
@property (assign, nonatomic) CGFloat scaleY;
@property (assign, nonatomic) CGPoint scaleXY;

@end

@implementation UIView (MCShorthand_DropPrefix)

MCSHORTHAND_PROPERTY(scaleX, ScaleX, CGFloat)
MCSHORTHAND_PROPERTY(scaleY, ScaleY, CGFloat)
MCSHORTHAND_PROPERTY(scaleXY, ScaleXY, CGPoint)

@end

#endif

clsource avatar Oct 05 '15 17:10 clsource

Thanks for the hint, I'll take a look!

leoschweizer avatar Oct 05 '15 17:10 leoschweizer

I love Smalltalk, too. I have FTGShorthand that is inspired by Magical Record shorthand syntax.

But I'm OK with the prefix category method, as it leaves no room for confliction

BTW, why not make it for Swift?

onmyway133 avatar Oct 27 '15 11:10 onmyway133

I have FTGShorthand that is inspired by Magical Record shorthand syntax.

Thanks, I'll take a look at that too. On first glimpse it looks like you are figuring that out at runtime? For this library, I would prefer a solution that works at compile time though.

But I'm OK with the prefix category method, as it leaves no room for confliction

Not sure what you mean by that, the proposal from clsource?

BTW, why not make it for Swift?

Well, two reasons:

  1. This project was created because I needed it myself, and since I also use Objective-C++ in my projects, I can't really use Swift yet.
  2. The idea of this project was to get as much functionality as possible with as little code as possible - that's why almost everything is implemented on the NSObject level. However, that won't work in Swift since the important collection types are actually structs and have no common ancestor. That makes it hard to follow a strict object-oriented approach like OpiniontedC does.

leoschweizer avatar Oct 27 '15 21:10 leoschweizer

@leoschweizer if that is the case, then I think @clsource answer fits you. By "prefix category method" I mean oc_map, oc_filter, ... are OK with me.

You may already know this, there's ObjectiveSugar that is used heavily by me, which implements lots of cool methods inspired by Ruby

onmyway133 avatar Oct 28 '15 03:10 onmyway133

You may already know this, there's ObjectiveSugar that is used heavily by me, which implements lots of cool methods inspired by Ruby

Yes, I used it as well in the beginning. But there were a couple things I didn't like about it, e.g. that it intentionally does not preserve the collection types (e.g. it always returns mutable Array instances) or the lacking support of lesser-known collection types (e.g. NSHashTable). There are even more competitors, e.g. Underscore.m, but each of the libraries I know is quite different to OpinionatedC in detail.

leoschweizer avatar Oct 28 '15 08:10 leoschweizer