distinct types and type identifier

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

distinct types and type identifier

Graeme Geldenhuys-2
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: distinct types and type identifier

ik-6
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

On Jan 30, 2008 11:30 AM, Graeme Geldenhuys <[hidden email]> wrote:

> 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  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



--
http://ik.homelinux.org/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: distinct types and type identifier

Graeme Geldenhuys-2
On 30/01/2008, ik <[hidden email]> 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.

Thanks Ido!


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: distinct types and type identifier

Peter Vreman
In reply to this post by Graeme Geldenhuys-2
> On 30/01/2008, ik <[hidden email]> 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.



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: distinct types and type identifier

Graeme Geldenhuys-2
On 30/01/2008, Peter Vreman <[hidden email]> wrote:
>
> 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/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal