PDA

View Full Version : About fpc syntax



User137
14-08-2010, 09:47 AM
1) Inheriting from multiple classes

Is it possible to for example do something like this (didn't work when tried):
type
TGameObject = class
public
name: string;
end;

TObjButton = class(TGameObject)
public
pressed: boolean;
end;

TGLObjectProperties = class
public
texture: string;
end;

TGLButton = class(TObjButton, TGLObjectProperties)
public
procedure Draw;
end;
So it would inherit properties from 2 sub-classes.

2) else-if stack

I have a long structure of if's like:
if s='new' then begin
// ...
end else if s='add' then begin

end else if s='remove' then begin
...
end;
The strings are fictional in example but idea same. Reading multiple "commands" from textfile where i want to keep the format human-readable, therefore i do not want to save the commands as numbers instead.

Is there a stack limit to if's? Compiler doesn't support real "elseif" command but does it internally do it? Should we be starting to get worried and request a string support for "case" structure? xx(
I know i could also do workaround like having a string-array indexed for the commands and go through the list to find matching number, and finally using that in "case" structure.

jdarling
14-08-2010, 05:38 PM
Multiple inheritance via objects is not supported in Pascal, but inheritance from multiple interfaces and object delegates are supported (try Google to learn more, as others have explained MUCH better then I can).

As for your set of else if's, no, there is no limit to the size. But, I might suggest the 1st issue of the Delphi Gamer mag that Will puts out. I covered Trie's in that article and they are the optimal (without getting into real lexers) way to handle this. In fact, I've used Trie's to implement generic lexers and tokenizers.

Your basically looking for command based scripting, a topic that was (IMHO) best explained in the book "Game Scripting Mastery", sure, its dated and written from a C prospective, but its the best starter book on the subject.

- Jeremy

User137
14-08-2010, 07:50 PM
No info or even hint about the topic anywhere but accidentally found this with google:
http://bugs.freepascal.org/view.php?id=16578
Contains code like:
ITestInterface = interface
function GetMyRefCount: Integer;
property MyRefCount: Integer read GetMyRefCount;
end;

TTestInterfacedObject = class(TInterfacedObject, ITestInterface)

About if's i was just wondering because they actually create a tree structure. If i indent the code "properly" it looks like:
if s='new' then
begin
// ...
end
else
if s='add' then begin

end
else
if s='remove' then begin
...
end;
Depending on implementation it can be like recursive function, they have stack size.
(Not that my little program has any real worries with measly 10 or so commands, just curious)

jdarling
14-08-2010, 08:45 PM
Answering in reverse order here. Your if structures actually don't have anything to do with stack size. They are simply compiled down to compare and jump statements. What this does mean (and the reason Trie's are SOOO much faster) is that you are doing the comparison of the full value every time until you get a "true" back. So; EAT and EAST both have to evaluate fully, but the Trie lets you say "I have an E, then A, then oh look a T". Hopefully that makes some sense. I've actually had to work on code that had several 100 if's back to back (it was a nightmare but it "worked"). I wouldn't condone this, if you start getting over 8-10 if's backed up its almost impossible for the "average" developer to follow the meaning.

Now, on interfaces; FPC is VERY similar to Delphi, so you can apply what you find about Delphi to FPC in this area:
Introductory stuff:
http://www.delphibasics.co.uk/Article.asp?Name=Interface
http://delphi.about.com/od/oopindelphi/a/delphi_oop5_3.htm

Delegates and Interfaces for multiple inheritance:
http://www.obsof.com/delphi_tips/pattern.html

BTW: I searched Google for "delphi interfaces multi inheritance tutorial" to get the above resources. Yes, you do have to do some digging in the results, but you can find links. Unfortunately the best article I know of on the subject was in the Delphi Magazine that is now out of print. If you can find someone with the digital version maybe they can send it to you. Otherwise, your going to have to do some digging and some work to figure it all out.

- Jeremy