Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-03

Class Identification?

23 messages · 12 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jon David Sawyer

Heya ^_^ Is it possible to identify the class type?

For example TypeOf(MyClass) = TypeOf(MyClass)?

Is it possible to register diffrent types of classes at startup and then identify them later?

This is probably a simple matter solvable with a few calls from the System unit that are well documented. But honestly.. I didn't see any! ^_^

Any hints/suggestions would be much appreciated.

Jon David Sawyer
to Anton Tichawa: hi, Jon! for descendants of TObject, there's a method 'ClassType', e. g. *** begin if…
Show quoted text 14 lines
for descendants of TObject, there's a method 'ClassType', e. g. *** begin if MyObject.ClassType = CCalculator then begin bla bla end else bla bla end; *** see reference manual - system unit. this only works if a descendant of TObject has been assigned to MyObject. Anton.

Thanks. ^_^ I just maid my base class a descendant of TObject and that took care of it!

Anton Tichawa
to Jon David Sawyer: Heya ^_^ Is it possible to identify the class type? For example TypeOf(MyClass) =…

hi, Jon!

Show quoted text 13 lines
Heya ^_^ Is it possible to identify the class type? For example TypeOf(MyClass) = TypeOf(MyClass)? Is it possible to register diffrent types of classes at startup and then identify them later? This is probably a simple matter solvable with a few calls from the System unit that are well documented. But honestly.. I didn't see any! ^_^ Any hints/suggestions would be much appreciated.

for descendants of TObject, there's a method 'ClassType', e. g.

***
begin
  if MyObject.ClassType = CCalculator then begin
    bla bla
  end else bla bla
end;
***

see reference manual - system unit. this only works if a descendant of TObject has been assigned to MyObject.

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Sebastian Günther
to Jon David Sawyer: Thanks. ^_^ I just maid my base class a descendant of TObject and that took care of it!
Jon David Sawyer wrote 16 lines
> for descendants of TObject, there's a method 'ClassType', e. g. > > *** > begin > if MyObject.ClassType = CCalculator then begin > bla bla > end else bla bla > end; > *** > > see reference manual - system unit. this only works if a descendant of > TObject has been assigned to MyObject. > > Anton. Thanks. ^_^ I just maid my base class a descendant of TObject and that took care of it!

btw, all classes are implicit descendants of TObject. If you don't specify any base class, the class will be derived from TObject directly:

type
  TMyClass = class
    procedure Something;
    ...
  end;

TMyClass is automatically inherited from TObject

Both postings sound like this wasn't clear...

- Sebastian

Jon David Sawyer
to Sebastian Günther
Show quoted text 15 lines
btw, all classes are implicit descendants of TObject. If you don't specify any base class, the class will be derived from TObject directly: type TMyClass = class procedure Something; ... end; TMyClass is automatically inherited from TObject Both postings sound like this wasn't clear... - Sebastian

Indeed.. I did not know this. ^_^ Thank you very much.

Sebastian Günther
to Anton Tichawa: Nor did I - Thank you. I'd like to ask several questions: 1. What is then the difference…
Anton Tichawa wrote
Nor did I - Thank you. I'd like to ask several questions: 1. What is then the difference between a class without ancestor, and a class inheriting from TObject?

There is no difference

2. Will TObject remain the "default ancestor" in the future?

Yes

Show quoted text 5 lines
3. Efficiency: Sometimes I use classes without ancestor, with the assumption, that this might be more efficient in cases where TObject's methods and fields are not necessary. It seems that assumption was wrong. To save resources, should I use explicite pointers to objects when I don't need the features inherited by the class TObject?

No, I won't use classes in such cases. You could use objects (i.e. use the old object model introduced in Turbo Pascal 5.5, instead of Delphi's object model), they won't add any overhead. OTOH I don't recommend to use the old objects anymore, because almost all new high-level units use classes, and mixing classes with objects will lead to really ugly code.

The methods of TObject don't add overhead. The memory usage is very low as well: Only a single pointer (VMT pointer), plus the data fields of your class. But the VMT data has to be stored in the executable, including some relocations. Constructing and destructing of TObject instances is somewhat slower that for the old objects, but I think you can neglect this in almost all cases.

- Sebastian

Unknown author

To expand on Sebastians answer...

2. Will TObject remain the "default ancestor" in the future?

Yes, beacuse the Object Pascal that FPC supports is based on Borland Delphi's Object Pascal language (which is in part related to the Object Pascal standard created by the ISO or ANSI, I forget whuch one exactly.) Borland now have the defacto industrial standard Object Pascal implementation in the version of Object Pascal supported by Delphi. There are at least 3 compilers that support it, targetting multiple platforms (Delphi/Kylix, FPC, Virtual Pascal, and there a number of developers working on PDA versions of Object Pascal), on 10+ platforms. The only way it could change would be for Borland to lead the way. I believe even Delphi.NET is able to see the .NET 'Object' to be a 'TObject'.

Show quoted text 5 lines
3. Efficiency: Sometimes I use classes without ancestor, with the assumption, that this might be more efficient in cases where TObject's methods and fields are not necessary. It seems that assumption was wrong. To save resources, should I use explicite pointers to objects when I don't need the features inherited by the class TObject?

Write a procedure or function that takes a 'self' pointer to the data. This is basically what methods are at the end of the day.

If you want better allocation performance, you can always refactor the memory allocation routines too. If FPC follows Delphi's model, there will be a record that holds the memort allocation routines. Have a look for the record type TMemoryManager that will probably be located in System or Sysinit unit. Altering the way these routines work can greatly improve speed and resource usage.

One other point.... you really should use 'is' and not 'MyObject.ClassType'. Surely:

 if ( myinst is TButton ) then begin
   ...
 end;

looks less ugly than:

  if ( myinst.ClassType = TButton ) then ... ;

This is held up by the statement int he Borland help:

Show quoted text 7 lines
function ClassType: Description: ClassType dynamically determines the type of an object. It is used internally by the is and as operators. Note: Applications should rarely, if ever, call ClassType directly. Instead, use the is or as operator.

Whether there is currently a good reason or it is just stylistic, I believe it's better to use the recognised syntax of a language rather than a convenient loophole. Just as TObject will stay with us forever now in it's current form, so will the 'is' and 'as' operators.

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/

Michael Van Canneyt
to Unknown author
Show quoted text 20 lines
Write a procedure or function that takes a 'self' pointer to the data. This is basically what methods are at the end of the day. If you want better allocation performance, you can always refactor the memory allocation routines too. If FPC follows Delphi's model, there will be a record that holds the memort allocation routines. Have a look for the record type TMemoryManager that will probably be located in System or Sysinit unit. Altering the way these routines work can greatly improve speed and resource usage. One other point.... you really should use 'is' and not 'MyObject.ClassType'. Surely: if ( myinst is TButton ) then begin ... end; looks less ugly than: if ( myinst.ClassType = TButton ) then ... ;

This is not entirely correct.
The latter will return false for a descendent of TButton, while the former will give true. The exact statement corresponding to the 'is' operator is

if myinst.Inheritsfrom(TButton) then ... ;

Just crossing the t's and dotting the i's...

Michael.

A.J. Venter
to Anton Tichawa: hi, Jon! for descendants of TObject, there's a method 'ClassType', e. g. *** begin if…

Just in case anybody needs some ammo against people who think pascal is dead.
This months Linux format includes AJ's Internet Cafe For LTSP (now DireqCafe) which was written in FPC.
Just goes to show, aint no proble it can't solve.

And before anybody acuses me of just downright spamming, yes I am the A.J. that wrote it, and no I am not trying to make anybody buy it, it's GPL anyway.
In case you do care, akinimod.sf.net, otherwize just ignore the post.

A.J.

"I'm a creationist. I refuse to believe that I could have evolved from man." A.J. Venter DireqLearn Linux Guru Public Key: http://www.mandrakesecure.net/en/cks/search.cgi?stype=keyid_4b&keyid_4b=F5AA10E9
Florian Klaempfl
to A.J. Venter
A.J. Venter wrote 12 lines
Just in case anybody needs some ammo against people who think pascal is dead. This months Linux format includes AJ's Internet Cafe For LTSP (now DireqCafe) which was written in FPC. Just goes to show, aint no proble it can't solve. And before anybody acuses me of just downright spamming, yes I am the A.J. that wrote it, and no I am not trying to make anybody buy it, it's GPL anyway. In case you do care, akinimod.sf.net, otherwize just ignore the post. A.J.

I think such a posting isn't off topic and it's interessting to hear about such things :)

Unknown author

Michael,

This is not entirely correct.

Neither is your explanation ;-)

The latter will return false for a descendent of TButton, while the former will give true. The exact statement corresponding to the 'is' operator is

"The is and as operators use InheritsFrom in their implementations. The is operator, however, can only determine the inheritance relationship of an instance."

So it would not be exactly the same in all instances.

Besides, the original poster missed the point of inheritence and polymorphism by asuming that classtype was a good way to to run time type checking. Consider the following generic example:

TTranslationType = (ttRaw, ttWav, ttMP3 ...);
TTranslator = class
  function Translate(sin, sout: TStream): boolean; virtual; abstract;
  function getTranslatorType: TTranslationType;
end;
TWAVTranslator = class(TTranslator)
  function Translate(sin, sout: TStream): boolean; override;
end;
TMP3Translator = class(TTranslator)
  function Translate(sin, sout: TStream): boolean; override;
end;
TTranslatorClass = class of TTranslator;
TTranslatorFactory = class
  ...
  procedure RegistreTranslator( newclass: TTranslatorClass );
  function  FindTranslator( translatorType: TTranslationType;
                            translator: TTranslator): boolean;
  ...
end;
var
  Factory: TTranslatorFactory = nil; //constructed in the initialization
...
var
  i, o: TFileStream;
  t: Translator;
  ...
begin
  Factory.RegisterTranslator(TWAVTranslator);
  Factory.RegisterTranslator(TMP3Translator);
  ...
  if ( Factory.FindTranslator( ttMP3, t) ) then begin
    t.Translate( i, o );
    ...
  end
  else
    raise Exception.Create('No Translator found');
  ...
end;
..

You don't *need* to know the type of the class instance the factory returns, just that it supports the functionality you requested... Interfaces make this much nicer ;-) However, if you use the _is_ operator, the code will be a lot less complex if you did need to find the class type. The other thing worth noting is that the _is_ and _as_ operators work the same with Interfaces as they do with classes... THIS is why you should use them.

if myinst.Inheritsfrom(TButton) then ... ;

The main difference is that it clearly stated in the D5 help that one should *not use* ClassType in the fashion indicated by the original poster.

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/

Anton Tichawa
to Jon David Sawyer: Indeed.. I did not know this. ^_^ Thank you very much.
you wrote 17 lines
> btw, all classes are implicit descendants of TObject. If you don't > specify any base class, the class will be derived from TObject directly: > > type > TMyClass = class > procedure Something; > ... > end; > > TMyClass is automatically inherited from TObject > > Both postings sound like this wasn't clear... > > > - Sebastian Indeed.. I did not know this. ^_^ Thank you very much.

Nor did I - Thank you. I'd like to ask several questions:

1. What is then the difference between a class without ancestor, and a class inheriting from TObject?

2. Will TObject remain the "default ancestor" in the future?

3. Efficiency: Sometimes I use classes without ancestor, with the assumption, that this might be more efficient in cases where TObject's methods and fields are not necessary. It seems that assumption was wrong. To save resources, should I use explicite pointers to objects when I don't need the features inherited by the class TObject?

Thanx,

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

James Mills
to Florian Klaempfl: I think such a posting isn't off topic and it's interessting to hear about such things :)
Florian Klaempfl wrote 16 lines
A.J. Venter wrote: >Just in case anybody needs some ammo against people who think pascal is >dead. >This months Linux format includes AJ's Internet Cafe For LTSP (now >DireqCafe) which was written in FPC. >Just goes to show, aint no proble it can't solve. > >And before anybody acuses me of just downright spamming, yes I am the >A.J. that wrote it, and no I am not trying to make anybody buy it, it's >GPL anyway. >In case you do care, akinimod.sf.net, otherwize just ignore the post. > >A.J. I think such a posting isn't off topic and it's interessting to hear about such things :)

Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead language, a language for kids. I'm happy to read of such a post. Just as a matter of interest, pascal might be an old language, a strongly typed language, but it is certainly NOT dead and any of my software engineer lecturers will agree with it's use as a language for developing software.

my 2 cents...

James

_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Anton Tichawa
to James Mills
you wrote 28 lines
On Mon, Mar 24, 2003 at 04:17:28PM +0100, Florian Klaempfl wrote: > A.J. Venter wrote: > >Just in case anybody needs some ammo against people who think pascal is > >dead. > >This months Linux format includes AJ's Internet Cafe For LTSP (now > >DireqCafe) which was written in FPC. > >Just goes to show, aint no proble it can't solve. > > > >And before anybody acuses me of just downright spamming, yes I am the > >A.J. that wrote it, and no I am not trying to make anybody buy it, it's > >GPL anyway. > >In case you do care, akinimod.sf.net, otherwize just ignore the post. > > > >A.J. > > I think such a posting isn't off topic and it's interessting to hear > about such things :) Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead language, a language for kids. I'm happy to read of such a post. Just as a matter of interest, pascal might be an old language, a strongly typed language, but it is certainly NOT dead and any of my software engineer lecturers will agree with it's use as a language for developing software. my 2 cents... James

i aggree that pascal lives. i see it that way, as blaise pascal says, mathematically, there might be a real big chance, and if you multiply that real big chance with a probabilty of 50 %, you'll get surely more than one. that's a kind of carrefour (french; in english it's crossroad, i think) of mystery and calculus. pascal will live as long as some programmers bear in mind something like:

***
begin
  repeat
    writeln('hello, world!');
    if keypressed() then begin
      bla bla
      ..
    end;
  until false;
end.
***

because that might be of value in the future. that's a common feature of pascal and rock'n'roll, isn't it?

anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Anton Tichawa
to James Mills: Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead…

hello, list!

you wrote 28 lines
On Mon, Mar 24, 2003 at 04:17:28PM +0100, Florian Klaempfl wrote: > A.J. Venter wrote: > >Just in case anybody needs some ammo against people who think pascal is > >dead. > >This months Linux format includes AJ's Internet Cafe For LTSP (now > >DireqCafe) which was written in FPC. > >Just goes to show, aint no proble it can't solve. > > > >And before anybody acuses me of just downright spamming, yes I am the > >A.J. that wrote it, and no I am not trying to make anybody buy it, it's > >GPL anyway. > >In case you do care, akinimod.sf.net, otherwize just ignore the post. > > > >A.J. > > I think such a posting isn't off topic and it's interessting to hear > about such things :) Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead language, a language for kids. I'm happy to read of such a post. Just as a matter of interest, pascal might be an old language, a strongly typed language, but it is certainly NOT dead and any of my software engineer lecturers will agree with it's use as a language for developing software. my 2 cents... James

i aggree that pascal lives. i see it that way, as blaise pascal says, mathematically, there might be a real big chance, and if you multiply that real big chance with a probabilty of 50 %, you'll get surely more than one. that's a kind of carrefour (french; in english it's crossroad, i think) of mystery and calculus. pascal will live as long as some programmers bear in mind something like:

***
begin
  repeat
    writeln('hello, world!');
    if keypressed() then begin
      bla bla
      ..
    end;
  until false;
end.
***

because that might be of value in the future. that's a common feature of pascal and rock'n'roll, isn't it?

anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Anton Tichawa
to James Mills: Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead…
you wrote 26 lines
On Mon, Mar 24, 2003 at 04:17:28PM +0100, Florian Klaempfl wrote: > A.J. Venter wrote: > >Just in case anybody needs some ammo against people who think pascal is > >dead. > >This months Linux format includes AJ's Internet Cafe For LTSP (now > >DireqCafe) which was written in FPC. > >Just goes to show, aint no proble it can't solve. > > > >And before anybody acuses me of just downright spamming, yes I am the > >A.J. that wrote it, and no I am not trying to make anybody buy it, it's > >GPL anyway. > >In case you do care, akinimod.sf.net, otherwize just ignore the post. > > > >A.J. > > I think such a posting isn't off topic and it's interessting to hear > about such things :) Absolutely. A lot of (up-them-selves) programmers tend to believe that pascal is a dead language, a language for kids. I'm happy to read of such a post. Just as a matter of interest, pascal might be an old language, a strongly typed language, but it is certainly NOT dead and any of my software engineer lecturers will agree with it's use as a language for developing software. my 2 cents...

btw - please can you explain that idiom - "my 2 cents"? thanx.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

James Mills
to Anton Tichawa
Anton Tichawa wrote 29 lines
On Monday 24 March 2003 23:16, you wrote: > On Mon, Mar 24, 2003 at 04:17:28PM +0100, Florian Klaempfl wrote: > > A.J. Venter wrote: > > >Just in case anybody needs some ammo against people who think pascal is > > >dead. > > >This months Linux format includes AJ's Internet Cafe For LTSP (now > > >DireqCafe) which was written in FPC. > > >Just goes to show, aint no proble it can't solve. > > > > > >And before anybody acuses me of just downright spamming, yes I am the > > >A.J. that wrote it, and no I am not trying to make anybody buy it, it's > > >GPL anyway. > > >In case you do care, akinimod.sf.net, otherwize just ignore the post. > > > > > >A.J. > > > > I think such a posting isn't off topic and it's interessting to hear > > about such things :) > > Absolutely. A lot of (up-them-selves) programmers tend to believe that > pascal is a dead language, a language for kids. I'm happy to read of > such a post. Just as a matter of interest, pascal might be an old > language, a strongly typed language, but it is certainly NOT dead and > any of my software engineer lecturers will agree with it's use as a > language for developing software. > > my 2 cents... btw - please can you explain that idiom - "my 2 cents"? thanx.

Would it sound better as: my contribution ?

cheers
James

Show quoted text 20 lines
---------- "Adas Methode war, wie sich zeigen wird, Tagtr?ume in offenbar korrekte Berechnungen einzuweben." Doris Langley Moore: Ada, Countess of Lovelace (London 1977). ---------- Anton Tichawa Volkertstrasse 19 / 20 A-1020 Wien mobil: +43 664 52 07 907 email: ---------- _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Mark Emerson
to James Mills
> btw - please can you explain that idiom - "my 2 cents"? thanx. Would it sound better as: my contribution ?

For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution".

(that's my 2 cents worth for today)
Mark

wz-i-like-linux

Hi, everybody

I can't master what on earth are you talking about, and do we need to argu about it? You see, there're more than 150 frequetly used languages (of course, most of them are not used large scope) in the world, why can't pascal have a position? The fact is, some people love free pascal, so it should exists. If you don't love it, leave it. Free pascal community is an open, freely community, if you love it, please help to construct it, if you don't love it, please don't destruct it.

Yours,
mili

A.J. Venter
to Mark Emerson: For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution". (that's…

Well thanks everybody for the words of encouragement and praise. I just released version 1.4 yesterday, with multi-language support etc.

I have been on this list since the days when you had to use a snapshot to get AnsiStrings, and it's always been great.
I know I don't post that often, but that's from working to much of late. I do read most of the messages and learn a lot all the time.

I have been using Pascal for over 10 years now, and I love it. I don't use or need any manuals for it.
But on this list, I am repeatedly astonished by the skill levels of the members which puts 10 years of hard-earned experience to is a true pity that most of the Academics I have met in my life belief, and teach their students, that Java is the one true language. Now java is falling out of fashion, an CS grads are suddenly finding it hard to get work, oh the irony.
Bjarn Stroustrop once said that only an idiot would do real work in a language that cannot write a compiler for itself.
I agree. All the more reason to cheer FPC.

My R0.02's worth (2c's South-African)
A.J.

Mark Emerson wrote 11 lines
> > btw - please can you explain that idiom - "my 2 cents"? thanx. > > Would it sound better as: my contribution ? For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution". (that's my 2 cents worth for today) Mark _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Bernhard Steffen
to Mark Emerson: For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution". (that's…
For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution".

and that is why some of us Europeans tend to write

"just my two Euro cents"
or
"just my two pence"

;-)

Bernhard

Show quoted text 10 lines
> > btw - please can you explain that idiom - "my 2 cents"? thanx. > > Would it sound better as: my contribution ? For all you Europeans, it is an American idiom meaning, "my HUMBLE contribution". (that's my 2 cents worth for today) Mark
Andrew Higgs

"A.J. Venter" wrote ..

<snip><snip>

My R0.02's worth (2c's South-African) A.J.

Hell, that's rather cheap hey (approximately 0.0025c American (about R8/$1))

Kind regards
Andrew Higgs