Sign in Join
Wiki copytemporary

Revision 2 of 3, by WikiArchive on 11-4-2026. The page as it is now.

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.

Contents

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

In Objective-C all dispatching is name-based, and hence a method with the same selector in a child class will always override a parent method with the same name (unlike in Object Pascal, it is not possible to hide an inherited method, nor to start a new inheritance tree).

Objective-Pascal however also requires override to be specified when overriding inherited Objective-C methods for consistency with Object Pascal. If override is absent, the compiler will give an error. This can also help catch bugs, as explained in this thread and this bug report filed with Apple. There is one exception though: for external classes, override is not required because such declarations are often produced using automated parsers, and requiring them to add override in the right places could needlessly complicate them. Not adding override in such situations will however still cause the compiler to show a hint.

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 the Internet Archive kept it on 2024-03-20. 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.