Archive for May, 2010

XPlatform Object Pascal

Sunday, May 23rd, 2010

I’ve followed FreePascal for some time now.  FreePascal has always been interesting in that their compiler architecture has supported multiple CPU targets for a long time, yet only now is Embarcadero working on such support.  Unfortunately, it looks like Embarcadero is writing their own from scratch rather than leveraging FPC, so a free, open source, and fully compatible compiler is not going to be available any time soon.

FPC supports multiple compatibility modes, and fpc libraries use similar if not the same names as library units from Delphi.  You would think porting a Delphi application to use FPC in order to get 64 bit Windows support or porting an application to Mac OS/X or Linux would be trivial.

The sad truth of the matter is that the moment if you want to port a Delphi app from Windows to a different OS you have to deal with UI toolkit differences and even on Windows porting to FPC can be a challenge because of the limited UI components and compiler compatibility.  That’s slowly changing with visual component frameworks like fpGUI, wxForms, and VGScene (assuming you used their framework to begin with).  Other components are being supplemented by a growing community via the Lazarus CCR, and vendors like REMObjects, and BRAIN PATCHWORK DX, LLC (the maker of DXSocks).  Lots of open source projects also support FPC, and the number seems to be growing.  If you’ve ever questioned the need for a cross platform Object Pascal compiler I think the community has already given it’s answer.

Tonight I tried to compile hcOPF using the latest version of FPC (2.4) on Mac OS/X.  I used the instructions on Donald Shimoda’s blog and it was absolutely painless.  I installed the Lazarus package for Mac OS/X and fired it up.

Initially I had some problems in my hcOPF port, figuring out why FPC couldn’t find my include files.  When I found out where to plug in the include file path, I thought I was off to the races until I got a “Could not find identifier TDate” error message.  It appears that on Mac OS/X FPC has a Controls unit, and so does Lazarus, but the FPC Unit is first in the path.  Once I got past that error, I discovered that the DBConsts unit in Delphi is named DBConst in FPC and it’s missing a number of resource strings.  After adding a whole bunch of IFDEFs, I hit a show stopper.  FPC doesn’t support the implements feature of the Delphi compiler to delegate the implementation of an interface to another object.  I use this in ThcObject to implement the IObserver interface.  This is a well known problem as documented here.  Unfortunately until this issue is solved, some of the best libraries will not be available to FPC developers.  I mean of course, the JVCL  ;>)

Inheriting Problems

Wednesday, May 5th, 2010

Recently I was working on a project in which I dropped a ThcUIObjectBinder component from the hcOPF framework onto a TFrame.  I put the frame onto a TForm container and kept getting a dialog in Delphi complaining that a ‘ThcUIBindingItem cannot be assigned to a ThcUIBindingItem’.  The error message doesn’t make much sense, but I had seen similar dialogs in the past, so I figured it came from the Assign() method.  I had not implemented an override in my ThcUIBindingItem class so it was using the default in TPersistent which generates the error dialog I had witnessed.

I checked out the DFM file for my form instance, and found that the bindings were empty, yet in the frame, I had two binding items.  This was also consistent with my supposition that it had to do with the Assign method.  When a component on a DFM effectively inherits it’s properties from a component in another DFM, it makes sense that the component is Assigned the properties of the “ancestor”  and then these properties are over written or augmented by the property values in the “descendant” DFM.  Of course the problem is that Assign() is virtual so you have no idea that the method must be overridden in some circumstances to get the behaviour you would expect because it isn’t documented in the help.

Sure enough, I added my own Assign() override and all is well.