Lightweight Generics

Availability

Compilers

“Objective-C 15 / 2.3”

  • Clang >= 3.7

    • Apple Clang >= 7

    • Xcode >= 7

Runtimes

No additional requirements.

Overview

Generics allow you to define types that operate on other types. For example, NSArray uses generics so that you can create an array holding only one element – for example you could create an array of cats.

Using Generic Classes

Compatibility Macros

Since GCC does not support lightweight generics, these macros are used in GNUstep to support lightweight generics.

These macros are provided by <Foundation/NSObjCRuntime.h> on GNUstep. If you need to port to Cocoa, you can copy-paste these to a header in your code.

#if __has_feature(objc_generics)
# define GS_GENERIC_CLASS(clz, ...) clz<__VA_ARGS__>
# define GS_GENERIC_TYPE_F(typeRef, fallback) typeRef
#else
# define GS_GENERIC_CLASS(clz, ...) clz
# define GS_GENERIC_TYPE_F(typeRef, fallback) fallback
#endif
#define GS_GENERIC_TYPE(typeRef) GS_GENERIC_TYPE_F(typeRef, id)

Compatibility macro

Meaning

Meaning without generics

GS_GENERIC_CLASS( clz , typename… )

clz < typename >

clz

GS_GENERIC_TYPE_F( typeRef , fallback )

typeRef

fallback

GS_GENERIC_TYPE( typeRef )

typeRef

id

References