Sign in Join

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

Mailing lists / fpc-devel / 2008-07

Illegal type conversion: "<enumeration type>" to "TObject"

18 messages · 13 people · –

Reply by email Subscribe to this list Your subscription

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

Sean McIlwain

The following code fails to compile in Delphi mode but successfully compiles in objfpc mode:

type
  TEnum = (a, b, c);
var
  AValue: TEnum;
  AObject : TObject;
implementation
initialization
  AObject := TObject(AValue);

Could this be supported in the Delphi mode as well?

Thanks,
Sean

Reply by email

Florian Klaempfl
to Sean McIlwain
Show quoted text 16 lines
The following code fails to compile in Delphi mode but successfully compiles in objfpc mode: type TEnum = (a, b, c); var AValue: TEnum; AObject : TObject; implementation initialization AObject := TObject(AValue); Could this be supported in the Delphi mode as well?

Please submit a bug report.

Reply by email

Michael Van Canneyt
to Florian Klaempfl
Florian Klaempfl wrote 19 lines
Sean McIlwain schrieb: > The following code fails to compile in Delphi mode but successfully compiles > in objfpc mode: > > type > TEnum = (a, b, c); > var > AValue: TEnum; > AObject : TObject; > implementation > > initialization > AObject := TObject(AValue); > > > > Could this be supported in the Delphi mode as well? Please submit a bug report.

Eh ?
As far as I'm concerned this should not compile in Objfpc mode as well ?

Michael.

Reply by email

Vinzent Höfler
to Micha Nelissen
Micha Nelissen wrote 10 lines
Michael Van Canneyt wrote: >>> AObject := TObject(AValue); >>> >>> Could this be supported in the Delphi mode as well? >> Please submit a bug report. > Eh ? > As far as I'm concerned this should not compile in Objfpc mode as well ? It's a typecast, so then almost anything is allowed, except perhaps if the size doesn't match.

The latter probably being the problem here, sizes of enumeration types differ in Delphi and FPC mode.

Apart from that I don't see any sense in such type of a typecast.

If I were to store such things in a "generic" list data type I would use a pointer list, not an object list, if alone to avoid the risk of accidentally trying to "Free" such objects.

Vinzent.

Reply by email

Boian Mitov
to Vinzent Höfler

Now only if we can go ~15 years back in time and tell that to the original developers of VCL and things like TStrings and their data type ;-) . Unfortunately they take TObject only, and there are a lot of cases when casting other data types to it is needed. I wish it was not the case, but it is the reality :-( .

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 14 lines
----- Original Message ----- From: "Vinzent Höfler" To: "FPC developers' list" Sent: Thursday, July 17, 2008 3:12 PM Subject: Re: [fpc-devel] Illegal type conversion: "<enumeration type>" to"TObject" > Apart from that I don't see any sense in such type of a typecast. > > If I were to store such things in a "generic" list data type I would use a > pointer list, not an object list, if alone to avoid the risk of > accidentally trying to "Free" such objects. >

Reply by email

Vinzent Höfler
to Boian Mitov
Boian Mitov wrote 5 lines
Now only if we can go ~15 years back in time and tell that to the original developers of VCL and things like TStrings and their data type ;-) . Unfortunately they take TObject only, and there are a lot of cases when casting other data types to it is needed. I wish it was not the case, but it is the reality :-( .

Hmm, point taken. Well, at least TStrings doesn't seem to add any functionality to further handle such "objects", especially it doesn't try "Free"ing any of them.

Show quoted text 13 lines
----- Original Message ----- From: "Vinzent Höfler" To: "FPC developers' list" Sent: Thursday, July 17, 2008 3:12 PM Subject: Re: [fpc-devel] Illegal type conversion: "<enumeration type>" to"TObject" > Apart from that I don't see any sense in such type of a typecast. > > If I were to store such things in a "generic" list data type I would > use a pointer list, not an object list, if alone to avoid the risk of > accidentally trying to "Free" such objects.

Vinzent.

Reply by email

Marco van de Voort
to Michael Van Canneyt: Eh ? As far as I'm concerned this should not compile in Objfpc mode as well ? Michael.
Show quoted text 17 lines
On Thu, 17 Jul 2008, Florian Klaempfl wrote: > > > > type > > TEnum = (a, b, c); > > var > > AValue: TEnum; > > AObject : TObject; > > implementation > > > > initialization > > AObject := TObject(AValue); > > Could this be supported in the Delphi mode as well? > > Please submit a bug report. Eh ? As far as I'm concerned this should not compile in Objfpc mode as well ?

It should compile if sizeof(tenum)=sizeof(tobject) else not? So probably also in Delphi this code depends on $Z ($minenum)

Reply by email

Schatzl Thomas

Hello,

Show quoted text 22 lines
Date: Fri, 18 Jul 2008 10:31:01 +0200 (CEST) From: (Marco van de Voort) > On Thu, 17 Jul 2008, Florian Klaempfl wrote: > > > > > > type > > > TEnum = (a, b, c); > > > var > > > AValue: TEnum; > > > AObject : TObject; > > > implementation > > > > > > initialization > > > AObject := TObject(AValue); > > > Could this be supported in the Delphi mode as well? > > > > Please submit a bug report. > > Eh ? > As far as I'm concerned this should not compile in Objfpc mode as well ? It should compile if sizeof(tenum)=sizeof(tobject) else not? So probably also in Delphi this code depends on $Z ($minenum)

This will never compile directly on 64 bit platforms (x64/ppc64) because afaik sizeof(enum) always sizeof(tobject) there. The original poster didn't mention his platform afaik.

Maybe use "AObject := TObject(PtrUInt(AValue));".

Minenumsize is not a solution, since the allowed values are only 1,2 or 4 according to the docs.

Regards,
Thomas

Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

Reply by email

Marco van de Voort
to Schatzl Thomas
Show quoted text 13 lines
> Date: Fri, 18 Jul 2008 10:31:01 +0200 (CEST) > > > > Eh ? > > As far as I'm concerned this should not compile in Objfpc mode as well ? > > It should compile if sizeof(tenum)=sizeof(tobject) else not? So probably > also in Delphi this code depends on $Z ($minenum) This will never compile directly on 64 bit platforms (x64/ppc64) because afaik sizeof(enum) always sizeof(tobject) there. The original poster didn't mention his platform afaik. Maybe use "AObject := TObject(PtrUInt(AValue));". Minenumsize is not a solution, since the allowed values are only 1,2 or 4 according to the docs.

It was not meant as a solution, but as a clarification of the behaviour, and to show that in some cases it can work, depending on $Z.

Reply by email

Craig Peterson
to Schatzl Thomas: This will never compile directly on 64 bit platforms (x64/ppc64) because afaik…
Schatzl Thomas wrote
This will never compile directly on 64 bit platforms (x64/ppc64) because afaik sizeof(enum) always sizeof(tobject) there. The original poster didn't mention his platform afaik.

Sorry, Sean was out of the office yesterday.

Boian hit it exactly; we're doing this cast to store values in the TStrings Objects[] property. The current platform we're targeting is Linux 32-bit, but we'll probably go to 64-bit at some point too, and it would be nice if the cast continued to work as is.

AFAIK, none of the original Delphi code uses MinEnumSize, so it does work there with 1 and 2 byte enumerations as well.

Craig

Reply by email

Пётр Косаревский
to Craig Peterson
Show quoted text 5 lines
Boian hit it exactly; we're doing this cast to store values in the TStrings Objects[] property. The current platform we're targeting is Linux 32-bit, but we'll probably go to 64-bit at some point too, and it would be nice if the cast continued to work as is. Craig

1. Why not use dummy class like "class tmyenum(tobject) e: myenumtype; end;"? (extra typing is something like "(objects[i] as tmyenum).e" instead of typecasting, but you can [have to] "Free" it: it's normal object)

2. By the way, compared to Delphi, FPC with common arguments wastes much more memory on large amounts of such objects.

Reply by email

Craig Peterson
to Пётр Косаревский
Пётр Косаревский wrote
1. Why not use dummy class like "class tmyenum(tobject) e: myenumtype; end;"? (extra typing is something like "(objects[i] as tmyenum).e" instead of typecasting, but you can [have to] "Free" it: it's normal object)

Why would that be better than a simple typecast? Typecasting to/from an object is a fast and easy way to associate an integer or enum with the strings in a TStrings object. We know when we're doing this, and TStrings never does anything with the object, so the alternatives add runtime and code overhead that simply isn't needed. Even if this was a one off thing I'd question introducing an object for it, but in our case there's literally dozens of spots where we use the technique with many different enumerated types.

Craig

Reply by email

Daniël Mantione
to Craig Peterson

Op Fri, 18 Jul 2008, schreef Craig Peterson:

Show quoted text 7 lines
???? ??????????? wrote: > 1. Why not use dummy class like "class tmyenum(tobject) e: myenumtype; > end;"? > (extra typing is something like "(objects[i] as tmyenum).e" instead of > typecasting, but you can [have to] "Free" it: it's normal object) Why would that be better than a simple typecast?

Because you know it is compatible with a Tobject.

Typecasting to/from an object is a fast and easy way to associate an integer or enum with the strings in a TStrings object.

As you have experienced it might be fast, but not easy, since you are making assumptions on the size of enums, causing portability issues between compilers and also platforms.

We know when we're doing this, and TStrings never does anything with the object, so the alternatives add runtime and code overhead that simply isn't needed.

True.

Even if this was a one off thing I'd question introducing an object for it, but in our case there's literally dozens of spots where we use the technique with many different enumerated types.

It's hacking around library limitations. Library limitations should not be fixed with language changes, but by fixing the libraries.

Daniël

Reply by email

Jonas Maebe
to Craig Peterson: Why would that be better than a simple typecast? Typecasting to/from an object is a fast…
Craig Peterson wrote 9 lines
Пётр Косаревский wrote: > 1. Why not use dummy class like "class tmyenum(tobject) e: > myenumtype; end;"? > (extra typing is something like "(objects[i] as tmyenum).e" instead > of typecasting, but you can [have to] "Free" it: it's normal object) Why would that be better than a simple typecast? Typecasting to/ from an object is a fast and easy way to associate an integer or enum with the strings in a TStrings object.

Note that this will break if you turn on class type checking (-CR). In other words, you cannot use class type checking to check/debug programs that use this trick.

Jonas

Reply by email

Marco van de Voort
to Jonas Maebe
Show quoted text 8 lines
On 18 Jul 2008, at 18:26, Craig Peterson wrote: > Why would that be better than a simple typecast? Typecasting to/ > from an object is a fast and easy way to associate an integer or > enum with the strings in a TStrings object. Note that this will break if you turn on class type checking (-CR). In other words, you cannot use class type checking to check/debug programs that use this trick.

Hmm, can't you turn that off locally?

Reply by email

Vincent Snijders
to Marco van de Voort
Marco van de Voort wrote 9 lines
> On 18 Jul 2008, at 18:26, Craig Peterson wrote: >> Why would that be better than a simple typecast? Typecasting to/ >> from an object is a fast and easy way to associate an integer or >> enum with the strings in a TStrings object. > Note that this will break if you turn on class type checking (-CR). In > other words, you cannot use class type checking to check/debug > programs that use this trick. Hmm, can't you turn that off locally?

Is there {$ObjectCheck OFF} ?

Vincent

Reply by email