Hi,
- Properties: I have them listed in my To-Do-list
(http://fjf.gnu.de/gpc-todo_toc.html), but I personally don't know too much about them. Are they a Delphi extension? If so, are there any documents available to describe them, or otherwise, could some Delphi user put together a list of features required that could be used as a "standard" that GPC, and perhaps FPK, can implement?
Well, they look like fields, feel like fields, but aren't fields. Properties are declared:
Property <name> : <type> read <howtoget> write <howtoset>;
Proprties can read/write directly to a field or use get/set methods:
Type TMyClass = Class (TObject) private FValue : Integer; public property Value : Integer Read FValue Write FValue; end;
using the object it can easily be accessed like a field:
MyObject.Value := 256; x := MyObject.Value;
Data is still encapsuated. It's easy to change the implementation. Let's assume that Value may not be > 255.
Type TMyClass = Class (TObject) private FValue : Integer; Procedure SetValue (Value: Integer); public property Value : Integer Read FValue Write SetValue; end;
Procedure TMyClass.SetValue (Value: Integer); begin If Value > 255 then Raise Exception.Create ('Value may not be > 255'); FValue := Value; end;
If the user now does
MyObject.Value := 256;
the assignment will fail with an exception.
- Classes: At http://pascal-central.com/OOE-stds.html, there is a
draft for an ANSI Object Pascal standard. AFAIH, Delphi's implementation is quite close to this standard. One of the plans for GPC is to implement this standard draft without dropping the BP object model that is already implemented, in the hope that there won't be great conflicts between the two.
They are quite close. The look the same at the first glance, but they are not compatible. For example, Delphi does not know Class-/Kinds/. Classes are always declared the same way: TNewClass = Class (TDerivedFrom); Encapsulation is done quite the same way as in BP:
TNewClass = Class (TObject) private // May not be accessed from outside (also not in subclasses) protected // May not be accessed from outside, but subclasses cann access public // May be accessed from outside published // A IDE-thing. should be handled the same way as "public" end;
Othere than the Ansi-Draft in Delphi not each method is virtual. Delphi supports "Virtual" and "Dynamic".
Procedure x; Virtual; Procedure y; Dynamic;
"override" is used to override the method;
Procedure x; override;
Create is the standard-constructor and destroy the standard-destructor. Objects are always on heap and the pointer is automatically dereferenced.
Objects are initialized the following way:
MyObject := TMyClass.Create;
and freed with
MyObject.Free;
Since they are automatically dereferenced it is not necessary to say MyObject^.Free;
One of Delphis's bith strengths are the runtime-type-informations. Delphi introduces "is" and "as".
"is" checks wheter the object is this or an subclass:
If TMyObject is TMyClass then do ...;
If TMyObject is a object with type TMyClass or a object of a subclass of TMyClass then the expression is true.
"as" is used for typeconversion:
try x := (MyObject as TMyClass).Value; except x := 0; end;
If the typeconversion to MyClass is not possible (not (MyObject is TMyClass)) as raises an exception.
There is much more to say about Delphis Class-Model, but these are the basics. In Delphi both models live together. The old model can be still used, but it is not possible to use the new model to subclass from an old-model-type object. But this is not necessary since in Delphi each class uses the new model.
- Exception handling: I myself would like to use it (for OOP as well
as non-OOP programs), so I'd be willing to put some effort into implementing it when I have the time (later this year, hopefully). However, as with properties, I'm lacking a (pseudo-)standard. I do know the Java exception handling, and I think it's not too bad and could be implemented similarly in Pascal. OTOH, I read that the latest GCC version (on which GPC is based) has introduced exception handling, so it might be easier to implement this if we can reuse
Exception handling - really nice thing in Delphi.
An exception is fired with raise. The information about the exception is stored in an object. The basic-exception-class is "Exception"
raise Exception.Create ('This was wrong');
To use store additional data (e.g. and errorcode) exception can be easily subclassed:
EMyException = Class (exception) private FErrorCode: Integer; public Constructor Create (Errorcode: Integer); property Errorcode : Integer Read FErrorcode; end;
Constructor EMyException.Create (Errorcode: Integer); begin FErrorCode := Errorcode; Inherited Create ('Error '+IntToStr(Errorcode)); end;
Exceptions are catched the following way:
try // code U try to execute except // handle the exception end;
Since exceptions are classes the different types of exceptions can be handled in "except"
try // code U try to execute except On EMyException do xxxx; // Do special exception handling here On EAnotherException do xxxx; // What you want else raise; // let the standard exception handler do the thing end;
"finally" is used to assure that some piece of code is always executed:
try // code U want to execute finally // Do it also if code fails end;
This is very usable to assure that resources are freed:
MyObject := TMyClass.Create; try x := MyObject.value / 0; // Ooops /0 exception here! finally MyObject.Free; // But the object will be freed. end;
that code. OTTH, if there already is an implementation in a Pascal dialect (Delphi?), and it's suitable, it would be a good idea to be compatible to this. So again, could someone make a list of required features (perhaps with some sample code)?
I totally agree. IMO GCC should be compatible with Delphis Object Pascal, since OP is the real standard (1000000+ sold copies of Delphi) and not only a paper-standard. But you guys do it for free, so everything is fine, but Delphi/OP compatiblity would be /great/.
If people come to Linux they could easily reuse existing Delphi/OP-code.
And yes. Delphi's implementation is /great/ even the new referece counted string types make live much easier. They can save more than > 255, are dynamically allocated and referece-counted. And can for type-conversion easyly typecased with PChar:
Procedure MyProc (xxx: PChar);
var mystring: String;
MyProc (PChar(MyString));
This type of string was introduced with Delphi 2.
There are much more things in Delphi/OP which make live easier. The big advantages I see at the moment:
- New class model with RTTI, Properties - Exception Handling - AnsiString and WideString (8bit and 16bit-char strings with > 255 chars, String is an AnsiString) - - Interfaces (not really necessary but nice)
You asked if there is an description of Delphi/OP. I think there is an helpfile available on www.interprise.com (I'll look if I can find it). But I doubt that it is possible to implement it compatible w/o having a look at Delphi itself. But I hope that I could explain some basic things.
And thank you guys for supporting (Object) Pascal.
Andreas
From: "Andreas Prucha [helicon]" prucha@helicon.co.at To: "gpc@hut.fi" gpc@hut.fi, "jola@csd.uu.se" jola@csd.uu.se Subject: Delphis Object Pascal (was: RE: Reformatted: EFLIB as class library for Object-Pascal (fwd)) Date: Fri, 8 May 1998 04:32:20 +0200 Organization: helicon software development
Andreas Prucha wrote [on Properties in Delphi]
Well, they look like fields, feel like fields, but aren't fields. Properties are declared:
Property <name> : <type> read <howtoget> write <howtoset>;
Proprties can read/write directly to a field or use get/set methods:
[....]
Frank and others, Andreas' article is a classic! I don't think that the Delphi Class model can be explained much better without writing a thesis. I certainly couldn't explain it better in as few words. Basically, it contains all the information that Frank wanted. I hope that this is sufficient for Frank's proposed work on this issue.
Best regards, The Chief -------- Dr. Abimbola A. Olowofoyeku (The African Chief) Email: laa12@keele.ac.uk Homepage: http://ourworld.compuserve.com/homepages/African_Chief/ Author of: Chief's Installer Pro 4.25 for Win16 and Win32: ftp://ftp.simtel.net/pub/simtelnet/win3/install/chief425.zip