Difference between revisions of "FPC PasCocoa/Differences"

From Lazarus wiki
Jump to navigationJump to search
(Marking methods as ''override'')
Line 21: Line 21:
 
* '''Description''': In Object Pascal, if you do not specify a parent class, a newly defined class will automatically inherit from ''TObject''. While in NeXTStep/Apple/GNUStep's Objective-C frameworks ''NSObject'' fulfills a similar role, there are also other root classes (such as ''NSProxy''). Since there is no unique root class in the Objective-C language, this means that if you do not specify a parent class, you will define a new root class.  
 
* '''Description''': In Object Pascal, if you do not specify a parent class, a newly defined class will automatically inherit from ''TObject''. While in NeXTStep/Apple/GNUStep's Objective-C frameworks ''NSObject'' fulfills a similar role, there are also other root classes (such as ''NSProxy''). Since there is no unique root class in the Objective-C language, this means that if you do not specify a parent class, you will define a new root class.  
 
* '''What to do''': Generally, you will want new classes to inherit from ''NSObject'' if they do not have to inherit from any other class.
 
* '''What to do''': Generally, you will want new classes to inherit from ''NSObject'' if they do not have to inherit from any other class.
 +
 +
 +
==Differences with Objective-C==
 +
 +
===Marking methods as ''override''===
 +
* '''Description''': In Object Pascal, if you want to override an inherited method then you have to add the ''override'' keyword to the declaration in the child class. Without ''override'', the inherited method will be hidden in the syntactic scope of the child class, but when casting the child class to the parent type and call that method, the parent's implementation will still be executed.<p>&nbsp;&nbsp;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 (it is not possible to hide an inherited method, nor to start a new inheritance tree).</p><p>&nbsp;&nbsp;In Objective-Pascal, we also require ''override'' to be specified when overriding inherited Objective-C methods for consistency with Object Pascal. This can also catch some errors, as explained in [http://lists.apple.com/archives/Xcode-users/2009/Aug/msg00251.html this thread]. There is however one exception: for ''external'' classes, the ''override'' is not required because such declarations are often produced using automated parsers, and requiring them to add ''override'' in the right places would needlessly complicate them. Not adding ''override'' in such situations will however still cause the compiler to print a hint.</p>
 +
* '''What to do''': Always add ''override;'' at the end of method declarations that override inherited methods.

Revision as of 13:35, 30 August 2009

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 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 ObjCPas and on the one hand Objective-C, and on the other hand Object Pascal.

Differences with Object Pascal

No constructors

  • Description: The Objective-C language has no notion of a formal constructor. Hence, the constructor keyword is not supported in an objcclass.
  • What to do: Construction of class instances in Objective-C usually happens explicitly in two stages: first the alloc class message is sent to the class type that you want to instantiate, and next you send an initXXX instance message to its result. This initXXX (with XXX dependent on the class type) message is called an initializer, and will initialise in the various fields.
  • Warning: By convention, initXXX methods free the allocated memory and return nil if the initialisation failed for some reason. This means that if you allocate and initialise an instance using two different statements, you have to assign the result of the initXXX method to your instance variable. And you always have to check for nil after calling an initXXX method before using the result if you are not certain that initialisation will always succeed!
  • More information:

No unique root class

  • Description: In Object Pascal, if you do not specify a parent class, a newly defined class will automatically inherit from TObject. While in NeXTStep/Apple/GNUStep's Objective-C frameworks NSObject fulfills a similar role, there are also other root classes (such as NSProxy). Since there is no unique root class in the Objective-C language, this means that if you do not specify a parent class, you will define a new root class.
  • What to do: Generally, you will want new classes to inherit from NSObject if they do not have to inherit from any other class.


Differences with Objective-C

Marking methods as override

  • Description: In Object Pascal, if you want to override an inherited method then you have to add the override keyword to the declaration in the child class. Without override, the inherited method will be hidden in the syntactic scope of the child class, but when casting the child class to the parent type and call that method, the parent's implementation will still be executed.

      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 (it is not possible to hide an inherited method, nor to start a new inheritance tree).

      In Objective-Pascal, we also require override to be specified when overriding inherited Objective-C methods for consistency with Object Pascal. This can also catch some errors, as explained in this thread. There is however one exception: for external classes, the override is not required because such declarations are often produced using automated parsers, and requiring them to add override in the right places would needlessly complicate them. Not adding override in such situations will however still cause the compiler to print a hint.

  • What to do: Always add override; at the end of method declarations that override inherited methods.