16

The ReactiveCocoa framework makes use of weakify and strongify macros, both of which are preceded by an '@' symbol.

Here's an example (From this file).

- (RACSignal *)rac_textSignal {
        @weakify(self);
        return [[[[RACSignal
                ... 
               ];
}

What is the significance of the at symbol that is a prefix to the macro name? (NOTE: I have checked the macro, and it is called 'weakify', not '@weakify', so it isn't just the macro name!).

The macro itself is defined here:

https://github.com/jspahrsummers/libextobjc/blob/master/extobjc/EXTScope.h#L45

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Thanks, but it is not a dup of that question. Weakify is not a language keyword. – ColinE Dec 31 '13 at 18:48
  • 2
    possible duplicate of [How can I use commercial at sign in Objective-C macro?](http://stackoverflow.com/questions/18599174/how-can-i-use-commercial-at-sign-in-objective-c-macro) – Martin R Dec 31 '13 at 19:50

2 Answers2

18

There is no special meaning to macros starting with an @. This is done in libextobjc to make the @weakify and @strongify macros seem more idiomatic with the rest of the language.

Technically, the @ is not part of the macro. The macro is just weakify or strongify. The actual body of the macro, though, is written such that it will not compile unless preceded with an @. This is done by adding an empty @autoreleasepool {} at the beginning of the macro, but stripping off the leading @.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • Thanks! Although personally I think this is confusing. Why make these macros look any more 'special' than others? – ColinE Dec 31 '13 at 19:07
  • 1
    just came here to say that's maybe the most clever use of autoreleasepool i've seen in a long time – jere Dec 29 '14 at 20:43
7

The @ isn't part of the macro. weakify is defined as:

#define weakify(...) \
    autoreleasepool {} \
    metamacro_foreach_cxt(ext_weakify_,, __weak, __VA_ARGS__)

So @weakify(self) becomes:

@autorelease {} metamacro_foreach_cxt(ext_weakify_,, __weak, self)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks! but why not include the @ in the macro? – ColinE Dec 31 '13 at 19:06
  • My only guess is that the macro author was trying to be slick making the macro to appear to require the `@` before the macro name. – rmaddy Dec 31 '13 at 19:29
  • Slick? Maybe, confusing ... definitely! Why try to make it look like a language feature when it isn't? Oh well. Thanks! – ColinE Dec 31 '13 at 19:38
  • My use of the term "slick" wasn't a compliment. :) I agree it's a bad idea. – rmaddy Dec 31 '13 at 19:41
  • 3
    @ColinE They perform a task that's really more at the language level (manipulating variable declarations) than your typical macro or function invocation, and the @ adds visual weight. It was also meant to draw parallels with `@synchronized`, etc., although obviously the usage is a bit different in practice. – Justin Spahr-Summers Jan 01 '14 at 18:45
  • @maddy don't know if using trick like that (`@autoreleasepool, @try @catch, ...`) to make the use of @ can have any performance impact? I remember nicklockwood advised not to use `@try @catch` in Objc – onmyway133 May 22 '14 at 03:16
  • @entropy Of course it has an impact. It's adding needless code just to get the `@` in the macro name. Whether the impact is meaningful is another question. It all depends on how it's used. – rmaddy May 22 '14 at 03:20