Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #331 from anayini/feature/generics-on-category-int…
Browse files Browse the repository at this point in the history
…erfaces

Adding generics on category interfaces
  • Loading branch information
zwaldowski committed Jan 22, 2016
2 parents b46e3e1 + cb92f71 commit 4cd0e1e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 76 deletions.
8 changes: 8 additions & 0 deletions BlocksKit/BKDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
# define __null_unspecified
#endif

#if __has_feature(objc_generics)
# define __GENERICS(class, ...) class<__VA_ARGS__>
# define __GENERICS_TYPE(type) type
#else
# define __GENERICS(class, ...) class
# define __GENERICS_TYPE(type) id
#endif

#if !defined(BK_INITIALIZER)
# if __has_attribute(objc_method_family)
# define BK_INITIALIZER __attribute__((objc_method_family(init)))
Expand Down
30 changes: 15 additions & 15 deletions BlocksKit/Core/NSArray+BlocksKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ NS_ASSUME_NONNULL_BEGIN
@see NSDictionary(BlocksKit)
@see NSSet(BlocksKit)
*/
@interface NSArray (BlocksKit)
@interface __GENERICS(NSArray, ObjectType) (BlocksKit)

/** Loops through an array and executes the given block with each object.
@param block A single-argument, void-returning code block.
*/
- (void)bk_each:(void (^)(id obj))block;
- (void)bk_each:(void (^)(ObjectType obj))block;

This comment has been minimized.

Copy link
@a2

a2 Jan 22, 2016

Collaborator

Shouldn't this be __GENERICS_TYPE(ObjectType) so it uses id if the objc_generics feature is unavailable?

This comment has been minimized.

Copy link
@zwaldowski

zwaldowski Jan 22, 2016

Author Collaborator

Almost certainly


/** Enumerates through an array concurrently and executes
the given block once for each object.
Expand All @@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, void-returning code block.
*/
- (void)bk_apply:(void (^)(id obj))block;
- (void)bk_apply:(void (^)(ObjectType obj))block;

/** Loops through an array to find the object matching the block.
Expand All @@ -57,15 +57,15 @@ NS_ASSUME_NONNULL_BEGIN
@return Returns the object, if found, or `nil`.
@see bk_select:
*/
- (nullable id)bk_match:(BOOL (^)(id obj))block;
- (nullable id)bk_match:(BOOL (^)(ObjectType obj))block;

/** Loops through an array to find the objects matching the block.
@param block A single-argument, BOOL-returning code block.
@return Returns an array of the objects found.
@see bk_match:
*/
- (NSArray *)bk_select:(BOOL (^)(id obj))block;
- (NSArray *)bk_select:(BOOL (^)(ObjectType obj))block;

/** Loops through an array to find the objects not matching the block.
Expand All @@ -79,7 +79,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, BOOL-returning code block.
@return Returns an array of all objects not found.
*/
- (NSArray *)bk_reject:(BOOL (^)(id obj))block;
- (NSArray *)bk_reject:(BOOL (^)(ObjectType obj))block;

/** Call the block once for each object and create an array of the return values.
Expand All @@ -91,14 +91,14 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, object-returning code block.
@return Returns an array of the objects returned by the block.
*/
- (NSArray *)bk_map:(id (^)(id obj))block;
- (NSArray *)bk_map:(id (^)(ObjectType obj))block;

/** Behaves like map, but doesn't add NSNull objects if you return nil in the block.
@param block A single-argument, object-returning code block.
@return Returns an array of the objects returned by the block.
*/
- (NSArray *)bk_compact:(id (^)(id obj))block;
- (NSArray *)bk_compact:(id (^)(ObjectType obj))block;

/** Arbitrarily accumulate objects using a block.
Expand All @@ -119,7 +119,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A block that takes the current sum and the next object to return the new sum.
@return An accumulated value.
*/
- (nullable id)bk_reduce:(nullable id)initial withBlock:(__nullable id (^)(__nullable id sum, id obj))block;
- (nullable id)bk_reduce:(nullable id)initial withBlock:(__nullable id (^)(__nullable id sum, ObjectType obj))block;

/**
Sometimes we just want to loop an objects list and reduce one property, where
Expand All @@ -143,7 +143,7 @@ NS_ASSUME_NONNULL_BEGIN
}];
*/
- (NSInteger)bk_reduceInteger:(NSInteger)initial withBlock:(NSInteger(^)(NSInteger result, id obj))block;
- (NSInteger)bk_reduceInteger:(NSInteger)initial withBlock:(NSInteger(^)(NSInteger result, ObjectType obj))block;

/**
Sometimes we just want to loop an objects list and reduce one property, where
Expand All @@ -168,7 +168,7 @@ NS_ASSUME_NONNULL_BEGIN
*/

- (CGFloat)bk_reduceFloat:(CGFloat)inital withBlock:(CGFloat(^)(CGFloat result, id obj))block;
- (CGFloat)bk_reduceFloat:(CGFloat)inital withBlock:(CGFloat(^)(CGFloat result, ObjectType obj))block;

/** Loops through an array to find whether any object matches the block.
Expand All @@ -187,7 +187,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, BOOL-returning code block.
@return YES for the first time the block returns YES for an object, NO otherwise.
*/
- (BOOL)bk_any:(BOOL (^)(id obj))block;
- (BOOL)bk_any:(BOOL (^)(ObjectType obj))block;

/** Loops through an array to find whether no objects match the block.
Expand All @@ -196,14 +196,14 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, BOOL-returning code block.
@return YES if the block returns NO for all objects in the array, NO otherwise.
*/
- (BOOL)bk_none:(BOOL (^)(id obj))block;
- (BOOL)bk_none:(BOOL (^)(ObjectType obj))block;

/** Loops through an array to find whether all objects match the block.
@param block A single-argument, BOOL-returning code block.
@return YES if the block returns YES for all objects in the array, NO otherwise.
*/
- (BOOL)bk_all:(BOOL (^)(id obj))block;
- (BOOL)bk_all:(BOOL (^)(ObjectType obj))block;

/** Tests whether every element of this array relates to the corresponding element of another array according to match by block.
Expand All @@ -218,7 +218,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A two-argument, BOOL-returning code block.
@return Returns a BOOL, true if every element of array relates to corresponding element in another array.
*/
- (BOOL)bk_corresponds:(NSArray *)list withBlock:(BOOL (^)(id obj1, id obj2))block;
- (BOOL)bk_corresponds:(NSArray *)list withBlock:(BOOL (^)(ObjectType obj1, id obj2))block;

@end

Expand Down
20 changes: 10 additions & 10 deletions BlocksKit/Core/NSDictionary+BlocksKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ NS_ASSUME_NONNULL_BEGIN
@see NSArray(BlocksKit)
@see NSSet(BlocksKit)
*/
@interface NSDictionary (BlocksKit)
@interface __GENERICS(NSDictionary, KeyType, ObjectType) (BlocksKit)

/** Loops through the dictionary and executes the given block using each item.
@param block A block that performs an action using a key/value pair.
*/
- (void)bk_each:(void (^)(id key, id obj))block;
- (void)bk_each:(void (^)(KeyType key, ObjectType obj))block;

/** Enumerates through the dictionary concurrently and executes
the given block once for each pair.
Expand All @@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A block that performs an action using a key/value pair.
*/
- (void)bk_apply:(void (^)(id key, id obj))block;
- (void)bk_apply:(void (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find the first key/value pair matching the block.
Expand All @@ -51,14 +51,14 @@ NS_ASSUME_NONNULL_BEGIN
@param block A BOOL-returning code block for a key/value pair.
@return The value of the first pair found;
*/
- (nullable id)bk_match:(BOOL (^)(id key, id obj))block;
- (nullable id)bk_match:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find the key/value pairs matching the block.
@param block A BOOL-returning code block for a key/value pair.
@return Returns a dictionary of the objects found.
*/
- (NSDictionary *)bk_select:(BOOL (^)(id key, id obj))block;
- (NSDictionary *)bk_select:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find the key/value pairs not matching the block.
Expand All @@ -72,15 +72,15 @@ NS_ASSUME_NONNULL_BEGIN
@param block A BOOL-returning code block for a key/value pair.
@return Returns a dictionary of all objects not found.
*/
- (NSDictionary *)bk_reject:(BOOL (^)(id key, id obj))block;
- (NSDictionary *)bk_reject:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Call the block once for each object and create a dictionary with the same keys
and a new set of values.
@param block A block that returns a new value for a key/value pair.
@return Returns a dictionary of the objects returned by the block.
*/
- (NSDictionary *)bk_map:(id (^)(id key, id obj))block;
- (NSDictionary *)bk_map:(id (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find whether any key/value pair matches the block.
Expand All @@ -92,7 +92,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A two-argument, BOOL-returning code block.
@return YES for the first time the block returns YES for a key/value pair, NO otherwise.
*/
- (BOOL)bk_any:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_any:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find whether no key/value pairs match the block.
Expand All @@ -101,14 +101,14 @@ NS_ASSUME_NONNULL_BEGIN
@param block A two-argument, BOOL-returning code block.
@return YES if the block returns NO for all key/value pairs in the dictionary, NO otherwise.
*/
- (BOOL)bk_none:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_none:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a dictionary to find whether all key/value pairs match the block.
@param block A two-argument, BOOL-returning code block.
@return YES if the block returns YES for all key/value pairs in the dictionary, NO otherwise.
*/
- (BOOL)bk_all:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_all:(BOOL (^)(KeyType key, ObjectType obj))block;

@end

Expand Down
24 changes: 12 additions & 12 deletions BlocksKit/Core/NSMapTable+BlocksKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

NS_ASSUME_NONNULL_BEGIN

@interface NSMapTable (BlocksKit)
@interface __GENERICS(NSMapTable, KeyType, ObjectType) (BlocksKit)

/** Loops through the maptable and executes the given block using each item.
@param block A block that performs an action using a key/value pair.
*/
- (void)bk_each:(void (^)(id key, id obj))block;
- (void)bk_each:(void (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find the first key/value pair matching the block.
Expand All @@ -24,14 +24,14 @@ NS_ASSUME_NONNULL_BEGIN
@param block A BOOL-returning code block for a key/value pair.
@return The value of the first pair found;
*/
- (nullable id)bk_match:(BOOL (^)(id key, id obj))block;
- (nullable id)bk_match:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find the key/value pairs matching the block.
@param block A BOOL-returning code block for a key/value pair.
@return Returns a maptable of the objects found.
*/
- (NSMapTable *)bk_select:(BOOL (^)(id key, id obj))block;
- (NSMapTable *)bk_select:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find the key/value pairs not matching the block.
Expand All @@ -45,15 +45,15 @@ NS_ASSUME_NONNULL_BEGIN
@param block A BOOL-returning code block for a key/value pair.
@return Returns a maptable of all objects not found.
*/
- (NSMapTable *)bk_reject:(BOOL (^)(id key, id obj))block;
- (NSMapTable *)bk_reject:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Call the block once for each object and create a maptable with the same keys
and a new set of values.
@param block A block that returns a new value for a key/value pair.
@return Returns a maptable of the objects returned by the block.
*/
- (NSMapTable *)bk_map:(id (^)(id key, id obj))block;
- (NSMapTable *)bk_map:(id (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find whether any key/value pair matches the block.
Expand All @@ -65,7 +65,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A two-argument, BOOL-returning code block.
@return YES for the first time the block returns YES for a key/value pair, NO otherwise.
*/
- (BOOL)bk_any:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_any:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find whether no key/value pairs match the block.
Expand All @@ -74,37 +74,37 @@ NS_ASSUME_NONNULL_BEGIN
@param block A two-argument, BOOL-returning code block.
@return YES if the block returns NO for all key/value pairs in the maptable, NO otherwise.
*/
- (BOOL)bk_none:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_none:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Loops through a maptable to find whether all key/value pairs match the block.
@param block A two-argument, BOOL-returning code block.
@return YES if the block returns YES for all key/value pairs in the maptable, NO otherwise.
*/
- (BOOL)bk_all:(BOOL (^)(id key, id obj))block;
- (BOOL)bk_all:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Filters a mutable dictionary to the key/value pairs matching the block.
@param block A BOOL-returning code block for a key/value pair.
@see <NSMapTable(BlocksKit)>bk_reject:
*/
- (void)bk_performSelect:(BOOL (^)(id key, id obj))block;
- (void)bk_performSelect:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Filters a mutable dictionary to the key/value pairs not matching the block,
the logical inverse to bk_select:.
@param block A BOOL-returning code block for a key/value pair.
@see <NSMapTable(BlocksKit)>bk_select:
*/
- (void)bk_performReject:(BOOL (^)(id key, id obj))block;
- (void)bk_performReject:(BOOL (^)(KeyType key, ObjectType obj))block;

/** Transform each value of the dictionary to a new value, as returned by the
block.
@param block A block that returns a new value for a given key/value pair.
@see <NSMapTable(BlocksKit)>bk_map:
*/
- (void)bk_performMap:(id (^)(id key, id obj))block;
- (void)bk_performMap:(id (^)(KeyType key, ObjectType obj))block;

@end

Expand Down
8 changes: 4 additions & 4 deletions BlocksKit/Core/NSMutableArray+BlocksKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ NS_ASSUME_NONNULL_BEGIN
@see NSArray(BlocksKit)
*/
@interface NSMutableArray (BlocksKit)
@interface __GENERICS(NSMutableArray, ObjectType) (BlocksKit)

/** Filters a mutable array to the objects matching the block.
@param block A single-argument, BOOL-returning code block.
@see <NSArray(BlocksKit)>bk_reject:
*/
- (void)bk_performSelect:(BOOL (^)(id obj))block;
- (void)bk_performSelect:(BOOL (^)(id ObjectType))block;

/** Filters a mutable array to all objects but the ones matching the block,
the logical inverse to bk_select:.
@param block A single-argument, BOOL-returning code block.
@see <NSArray(BlocksKit)>bk_select:
*/
- (void)bk_performReject:(BOOL (^)(id obj))block;
- (void)bk_performReject:(BOOL (^)(id ObjectType))block;

/** Transform the objects in the array to the results of the block.
Expand All @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN
@param block A single-argument, object-returning code block.
@see <NSArray(BlocksKit)>bk_map:
*/
- (void)bk_performMap:(id (^)(id obj))block;
- (void)bk_performMap:(id (^)(id ObjectType))block;

@end

Expand Down
Loading

0 comments on commit 4cd0e1e

Please sign in to comment.