Sign in Join
Wiki copytemporary

FPC PasCocoa/Differences

FPC's Objective-Pascal dialect was introduced to enable seamless interfacing with Objective-C code, in particular on macOS. Due to inherent differences between the C and Pascal philosophies, this language mode will have some seemingly strange behaviour regardless of whether your main background is in Objective-C or in Object Pascal.

This page describes the conceptual differences between Objective-Pascal and on the one hand Objective-C, and on the other hand Object Pascal.

Differences with Object Pascal

No constructors

pascal
{$modeswitch objectivec1}
var
  obj: NSObject;
begin
  // 1. Initialisation in two steps. First allocate the memory.
  obj:=NSObject.alloc;
  // Next, initialise. Warning: do not just use "obj.init", since in theory it could
  // return nil, or even a completely different object!
  obj:=obj.init;
  obj.release;

  // 2. Alternative one-liner.
  obj:=NSObject.alloc.init;
  obj.release;
end.

No destructors

No unique root class

Differences with Objective-C

Marking methods as override

Class instances are implicit pointers

Protocol method signatures in class declarations

Duplicate identifiers

pascal
type
  NSObjectProtocol = objcprotocol external name 'NSObject' // specify the real name of this protocol
     // "_class" instead of "class" on the Pascal side, because "class" is a language keyword
     function _class: pobjc_class; message name 'class';
  end;

  NSObject = objcclass external (NSObjectProtocol) // name specification is optional since NSObject is the real name
    function _class: pobjc_class; // no need to specify the selector again, it's copied by the compiler from the protocol definition
    // "classClass" (or something else) instead of "_class" because otherwise there are
    // two methods with the same name in the NSObject class.
    class function classClass: pobjc_class; message 'class';
  end;

Using class types as first class entities

Sending class messages to instances

Instances that conform to protocols

Sending messages to id

Class extensions


From the Free Pascal wiki, page "FPC PasCocoa/Differences", as it stood on 2021-03-11 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.