ImportObjC pragma

Similar to the importc pragma for C, the importobjc pragma can be used to import Objective C methods. The generated code then uses the Objective C method calling syntax: [obj method param1: arg]. In addition with the header and emit pragmas this allows sloppy interfacing with libraries written in Objective C:

  1. # horrible example of how to interface with GNUStep ...
  2. {.passL: "-lobjc".}
  3. {.emit: """
  4. #include <objc/Object.h>
  5. @interface Greeter:Object
  6. {
  7. }
  8. - (void)greet:(long)x y:(long)dummy;
  9. @end
  10. #include <stdio.h>
  11. @implementation Greeter
  12. - (void)greet:(long)x y:(long)dummy
  13. {
  14. printf("Hello, World!\n");
  15. }
  16. @end
  17. #include <stdlib.h>
  18. """.}
  19. type
  20. Id {.importc: "id", header: "<objc/Object.h>", final.} = distinct int
  21. proc newGreeter: Id {.importobjc: "Greeter new", nodecl.}
  22. proc greet(self: Id, x, y: int) {.importobjc: "greet", nodecl.}
  23. proc free(self: Id) {.importobjc: "free", nodecl.}
  24. var g = newGreeter()
  25. g.greet(12, 34)
  26. g.free()

The compiler needs to be told to generate Objective C (command objc) for this to work. The conditional symbol objc is defined when the compiler emits Objective C code.