Sign in Join

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

Mailing lists / fpc-pascal / 2008-01

distinct types and type identifier

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

Below is from the Kylix 3 help.

--------------------------[ copy ]------------------------------
Type identity is almost straightforward. When one type identifier is
declared using another type identifier, without qualification, they
denote the same type. Thus, given the declarations
type
  T1 = Integer;
  T2 = T1;
  T3 = Integer;
  T4 = T2;

T1, T2, T3, T4, and Integer all denote the same type. To create distinct types, repeat the word type in the declaration. For example, type TMyInteger = type Integer;creates a new type called TMyInteger which is not identical to Integer.
--------------------------[ end ]------------------------------

So when am I supposed to use

type
  TfpgColor = longword;

vs

type
  TfpgColor = type longword;

???
What's the actual difference between them? I have used the first option (declaration) in the fpGUI project. For things like:

const
  clBlue = TfpgColor($0000FF);

Is the above the same as what the Free Pascal documentation describes below? Or is this something different?

--------------------------[ copy ]------------------------------ The last class, type identifier, is just a means to give another name to a type. This presents a way to make types platform independent, by only using these types, and then defining these types for each platform individually. The programmer that uses these units doesn't have to worry about type size and so on.
--------------------------[ end ]------------------------------

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

ik
to Graeme Geldenhuys
type
  TfpgColor = type longword;

means that you have a new type in the same *range* of longword. That means that you can assign the same number range, but if you have a variable that is a longword, and you wish to assigned the value to a TfpgColor, then you must cast it, or the compiler will tell you that it expect a TfpgColor content (unless there is a bug ;)).

Ido

Graeme Geldenhuys wrote 61 lines
Hi, Below is from the Kylix 3 help. --------------------------[ copy ]------------------------------ Type identity is almost straightforward. When one type identifier is declared using another type identifier, without qualification, they denote the same type. Thus, given the declarations type T1 = Integer; T2 = T1; T3 = Integer; T4 = T2; T1, T2, T3, T4, and Integer all denote the same type. To create distinct types, repeat the word type in the declaration. For example, type TMyInteger = type Integer;creates a new type called TMyInteger which is not identical to Integer. --------------------------[ end ]------------------------------ So when am I supposed to use type TfpgColor = longword; vs type TfpgColor = type longword; ??? What's the actual difference between them? I have used the first option (declaration) in the fpGUI project. For things like: const clBlue = TfpgColor($0000FF); Is the above the same as what the Free Pascal documentation describes below? Or is this something different? --------------------------[ copy ]------------------------------ The last class, type identifier, is just a means to give another name to a type. This presents a way to make types platform independent, by only using these types, and then defining these types for each platform individually. The programmer that uses these units doesn't have to worry about type size and so on. --------------------------[ end ]------------------------------ Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Graeme Geldenhuys
to ik
ik wrote 8 lines
type TfpgColor = type longword; means that you have a new type in the same *range* of longword. That means that you can assign the same number range, but if you have a variable that is a longword, and you wish to assigned the value to a TfpgColor, then you must cast it, or the compiler will tell you that it expect a TfpgColor content (unless there is a bug ;)).

Ah, okay. Now why can't they explain it like you just did, but in the help. :) Where could I add this explanation in the FPC docs?

I think I'll use the type as define above from now onwards... That way we should always know what we are working with (TfpcColor in this case) when assigning values to variables etc.

Thanks Ido!

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Peter Vreman
Show quoted text 17 lines
On 30/01/2008, ik wrote: > type > TfpgColor = type longword; > > means that you have a new type in the same *range* of longword. That > means that you can assign the same number range, but if you have a > variable that is a longword, and you wish to assigned the value to a > TfpgColor, then you must cast it, or the compiler will tell you that > it expect a TfpgColor content (unless there is a bug ;)). Ah, okay. Now why can't they explain it like you just did, but in the help. :) Where could I add this explanation in the FPC docs? I think I'll use the type as define above from now onwards... That way we should always know what we are working with (TfpcColor in this case) when assigning values to variables etc.

In case of unique integer types. The types are not the equivalent, but are still compatible and will be converted implicitly by the compiler. It unique types affect procedure overloading. The following short example demonstrates this.

type
  TfpgColor = type longword;
procedure f(c:TfpgColor);
begin
end;
procedure f(l:longword);
begin
end;
procedure f2(l:longword);
begin
end;
var
  c : TfpgColor;
  l : longword;
begin
  c:=l;
  f(c);
  f2(c);
end.

Reply by email

Graeme Geldenhuys
to Peter Vreman
Peter Vreman wrote 6 lines
In case of unique integer types. The types are not the equivalent, but are still compatible and will be converted implicitly by the compiler. It unique types affect procedure overloading. The following short example demonstrates this.

Thanks Peter. Your example confirms that defining it as a unique type is what I wanted. All methods that took a TfpgColor as parameter was defined using a TfpgColor type and not a LongWord. I prefer the idea of have TfpgColor defined as a unique type.

Thanks for all the replies...

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email