Sign in Join
Wiki copytemporary

FPC PasCocoa/Differences

Sent here from ObjCPas.

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 .

Edited on wiki.freepascal.org by 7 people, 2009–2021

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
JonasNew page: =Introduction= FPC's "Objective-Pascal" (ObjCPas) dialect was introduced to enable seamless interfacing with Objective-C code, in particular on Mac OS X. Due to inhe...
JonasObjCPas moved to FPC PasCocoa/Differences: Page documented the dialect itself is called FPC_PasCocoa (minor)
JonasNo constructors
JonasMarking methods as ''override''
JonasNo destructors
JonasNo constructors (minor)
JonasMarking methods as ''override'' (minor)
JonasDifferences with Objective-C
JonasClass instances are implicit pointers (minor)
JonasNo constructors: - added example
JonasNo destructors (minor)
JonasDuplicate identifiers (minor)
JonasNo constructors (minor)
JonasDifferences with Objective-C: - protocol method declarations have to be repeated in Objective-C class declarations
JonasDifferences with Objective-C: -- class types, class methods
JonasUsing class types as first class entities (minor)
JonasSending class messages to instances (minor)
JonasSending class messages to instances (minor)
JonasMarking methods as ''override'' (minor)
JonasDuplicate identifiers (minor)
JonasVariables that conform to protocols: -- added
JonasInstances that conform to protocols: -- moved to correct location (minor)
JonasIntroduction (minor)
JonasIntroduction (minor)
JonasMarking methods as ''override'' (minor)
JonasClass instances are implicit pointers (minor)
JonasNo constructors: - sending messages to ID is now allowed
JonasDifferences with Objective-C
JonasSending messages to ''id'' (minor)
JonasInstances that conform to protocols (minor)
JonasDifferences with Objective-C: - Class extensions
JonasClass extensions (minor)
JonasClass extensions (minor)
Skalogryz (minor)
JonasSending class messages to instances (minor)
JonasInstances that conform to protocols: workaround for specifying a parameter/variable/field that conforms to multiple protocols
Vincent
JonasObjCPas -> Objective-Pascal (minor)
JonasDuplicate identifiers: updated for new 'external' syntax
MischiCategory Mac OS X added (minor)
Penilopa PInstances that conform to protocols
Jonassyntax highlighting (minor)
Jwdietrich
SkalogryzSending messages to id (minor)
TrevRationalised macOS categories (minor)
TrevFixed syntax highlighting (minor)
Skalogryzno need for Introduction (minor)
TrevNo constructors: Fix broken Apple doc link (minor)
TrevNo constructors: Fix more broken links (minor)

Talk about this page

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