Discussion:
[fpc-pascal] FPC PasCocoa/Differences
Adriaan van Os via fpc-pascal
2021-03-27 18:11:38 UTC
Permalink
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

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.f
Jonas Maebe via fpc-pascal
2021-03-27 18:19:19 UTC
Permalink
    "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
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailm
Adriaan van Os via fpc-pascal
2021-03-27 19:31:07 UTC
Permalink
Post by Jonas Maebe via fpc-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)."
That makes it rather useless indeed. Thanks for the reply.

Regards,

Adriaan van Os

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fp

Loading...