[fpc-pascal] Class vs Object type

Richard Ward roward at mac.com
Sat Apr 9 16:12:15 CEST 2016

<<Paraphrasing Graeme’s and other’s previous emails. Please refer to the posted emails and all the responses if you need to.>>

> I use Class and Object data types frequently in my code. I like the fact
> that Object data types can be used without first instantiating an
> instance and it gets freed automatically.
> <snip>
> It has to be said that for more complex "objects" I always use a Class
> type though. Also, Delphi compatibility (or their lack of Object type
> support) is of no concern to me.
> <snip>
> Are there pros or cons to either?
When I first decided to explore OOP in FPC, I (as well as most others) discovered that FPC supported two different Object models and wondered the same.  In those days I had some free time to explore and ended up creating a Free Pascal Wiki article : 

Programming Using Objects - Free Pascal wiki <http://wiki.freepascal.org/Programming_Using_Objects>

Unfortunately I ran out of time and never got to the Class based version.  I started out liking the Object model for exactly the same reason you state, due to the simpler overhead of not having to “worry” as much about memory leak and initialization coding.  In short, I abandoned the Object model since most of the FPC support was for the Class model and mixing Class and Object models in the same code base can get confusing resulting in more types of bugs and increased time spent maintaining stuff.  Also, there are other (sometimes subtle) differences in how the Object and Class models are implemented.  For production code, my opinion is to avoid mixing models.

There was an offshoot discussion about reference vs. value types and your original comment above seems to be a common general consensus that one gravitates towards value types for simple structures and class (reference) types for more complex information where “identity” aspects are considered.  About two years ago, I decided I needed to learn another language and started exploring Ruby and Python.  About 3 months after surveying those languages, Apple unveiled Swift and I decided I would bite the bullet and delve into it.  If anyone wants to hear summaries of what I have learned, I would be happy to take a discussion over to FPC-other or private email.  

I mention my extended Swift dalliance here because one of the major internal dichotomies in the Swift language itself is reference vs value languages constructs superimposed with mutable and immutable keywords.  You may find the following article helpful which discusses how to determine when to use value vs. reference types.  It uses Swift examples but the analysis is generic.

Should I use a Swift struct or a class?
http://faq.sealedabstract.com/structs_or_classes/ <http://faq.sealedabstract.com/structs_or_classes/>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160409/03b15502/attachment.html>

More information about the fpc-pascal mailing list