Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-03

FPC PasCocoa/Differences

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Adriaan van Os

The FPC PasCocoa/Differences wiki <https://wiki.lazarus.freepascal.org/FPC_PasCocoa/Differences> writes about ObjC class extensions:

"You can use regular (non-external) categories defined in the implementation of a unit instead of class extensions, as the effect will be the same."

However, the Apple Programming with Objective-C manual writes here <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html>

"Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties."

and

"The only way to add a traditional property—backed by a new instance variable—to an existing class is to use a class extension, as described in Class Extensions Extend the Internal Implementation."

So, the FPC PasCocoa/Differences wiki seems to ignore a crucial difference between ObjC categories and ObjC class extensions, namely the ability to add properties to an existing class.

Am I correct that the latter is not possible in Objective-Pascal ? So, for example, I want to add a pointer field to NSView and all used classes inheriting from it. I could of course subclass NSView and used the classes inheriting from it, but that would be rather clumsy.

Regards,

Adriaan van Os

Jonas Maebe
to Adriaan van Os
Adriaan van Os wrote 7 lines
    "The only way to add a traditional property—backed by a new instance variable—to an existing class is to use a class extension, as described in Class Extensions Extend the Internal Implementation." So, the FPC PasCocoa/Differences wiki seems to ignore a crucial difference between ObjC categories and ObjC class extensions, namely the ability to add properties to an existing class.

I'm not sure that was already possible when that wiki page was written. It was probably only added later in Objective-C 2.0.

Am I correct that the latter is not possible in Objective-Pascal ?

You are correct. OTOH, it feels kind of useless in general, because

"A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time (the class is compiled at the same time as the class extension)."

So there's not really much difference between a class extension with fields and adding private fields to the class declaration itself. It's more something to keep information completely out of the header files in Objective-C. While you can use include files to separate your header and implementation of a unit in Pascal, and then only distribute the header include files so people can look at them, this feels like a rather niche use case.

Jonas

Adriaan van Os
to Jonas Maebe
Jonas Maebe wrote 5 lines
You are correct. OTOH, it feels kind of useless in general, because "A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time (the class is compiled at the same time as the class extension)."

That makes it rather useless indeed. Thanks for the reply.

Regards,

Adriaan van Os