Sign in Join

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

Mailing lists / fpc-devel / 2006-10

type question

13 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

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

Marc Weustink

Hi,

if I define 2 types like:

type
  MyA = type string;
  MyB = type string;

are MyA and MyB considered as the same type ?
Should it be allowed to assign a variable of type MyA to a variable of type MyB ?
IIRC, the use of = type <some type> creates a new type. Or am I wrong ?

Marc

Reply by email

Christian Iversen
to Marc Weustink
Marc Weustink wrote 9 lines
Hi, if I define 2 types like: type MyA = type string; MyB = type string; are MyA and MyB considered as the same type ?

No, you are explicitly marking them as a new type. This is a very cool feature of Pascal you wont find in many other languages.

(For instance, you could use it to create a new integer-type for little- and big-endian numbers, ensuring that you _never_ directly assign a little-endian number to a big-endian one, or vice versa)

Should it be allowed to assign a variable of type MyA to a variable of type MyB ?

No.

IIRC, the use of = type <some type> creates a new type.

That's right.

Regards, Christian Iversen

Reply by email

Marc Weustink
to Christian Iversen
Christian Iversen wrote 26 lines
On Saturday 14 October 2006 15:55, Marc Weustink wrote: > Hi, > > if I define 2 types like: > > type > MyA = type string; > MyB = type string; > > are MyA and MyB considered as the same type ? No, you are explicitly marking them as a new type. This is a very cool feature of Pascal you wont find in many other languages. (For instance, you could use it to create a new integer-type for little- and big-endian numbers, ensuring that you _never_ directly assign a little-endian number to a big-endian one, or vice versa) > Should it be allowed to assign a variable of type MyA to a variable of > type MyB ? No. > IIRC, the use of = type <some type> creates a new type. That's right.

OK, thanks for confirming my thoughts.
In an attempt to make some use of it, I tried the following, and it compiles without a problem (also tried with Integer).

From this it seems that for FPC it doesn't matter if it is "typed" or not.

Marc

program TypeTest;
{$mode objfpc}{$h+}
type
  TMyA = type String;
  TMyB = type String;
var
  A: TMyA;
  B: TMyB;
  S: String;
begin
  S := 'Some value';
  A := S;
  B := S;
  S := A;
  S := B;
  A := B;
  B := A;
end.

Reply by email

Ivo Steinmann
to Marc Weustink
Marc Weustink wrote 25 lines
program TypeTest; {$mode objfpc}{$h+} type TMyA = type String; TMyB = type String; var A: TMyA; B: TMyB; S: String; begin S := 'Some value'; A := S; B := S; S := A; S := B; A := B; B := A; end.

Well, assign works, but if you use them as var Parameters it works not! It's also nice if you have got overloaded functions.

-Ivo

Reply by email

Peter Vreman
to Christian Iversen: No, you are explicitly marking them as a new type. This is a very cool feature of Pascal…
Show quoted text 20 lines
On Saturday 14 October 2006 15:55, Marc Weustink wrote: > Hi, > > if I define 2 types like: > > type > MyA = type string; > MyB = type string; > > are MyA and MyB considered as the same type ? No, you are explicitly marking them as a new type. This is a very cool feature of Pascal you wont find in many other languages. (For instance, you could use it to create a new integer-type for little- and big-endian numbers, ensuring that you _never_ directly assign a little-endian number to a big-endian one, or vice versa)

For the compiler the types are not equal anymore, but they are still compatible for implicit type conversion. This has mainly impact on overload choosing and parameter passing. Normal assignments using ':=' are not affected because of the still available implicit type conversion.

Reply by email

Marc Weustink
to Peter Vreman
Peter Vreman wrote 24 lines
> On Saturday 14 October 2006 15:55, Marc Weustink wrote: >> Hi, >> >> if I define 2 types like: >> >> type >> MyA = type string; >> MyB = type string; >> >> are MyA and MyB considered as the same type ? > No, you are explicitly marking them as a new type. This is a very cool > feature > of Pascal you wont find in many other languages. > > (For instance, you could use it to create a new integer-type for little- > and > big-endian numbers, ensuring that you _never_ directly assign a > little-endian > number to a big-endian one, or vice versa) For the compiler the types are not equal anymore, but they are still compatible for implicit type conversion. This has mainly impact on overload choosing and parameter passing. Normal assignments using ':=' are not affected because of the still available implicit type conversion.

I came to this when I wanted to do operator overloading on them. The compiler complained that you cannot overload for equal types.

So I wanted first to know if it are equal types or not.

Next question: Should operator overloading be possible on such types ?

Marc

Reply by email

peter green
to Marc Weustink

one trick if you wan't to keep a type but redefine the operators on it is to wrap it in a single element record.

Show quoted text 49 lines
-----Original Message----- From: Behalf Of Marc Weustink Sent: 14 October 2006 16:42 To: FPC developers' list Subject: Re: [fpc-devel] type question Peter Vreman wrote: >> On Saturday 14 October 2006 15:55, Marc Weustink wrote: >>> Hi, >>> >>> if I define 2 types like: >>> >>> type >>> MyA = type string; >>> MyB = type string; >>> >>> are MyA and MyB considered as the same type ? >> No, you are explicitly marking them as a new type. This is a very cool >> feature >> of Pascal you wont find in many other languages. >> >> (For instance, you could use it to create a new integer-type for little- >> and >> big-endian numbers, ensuring that you _never_ directly assign a >> little-endian >> number to a big-endian one, or vice versa) > > For the compiler the types are not equal anymore, but they are still > compatible for implicit type conversion. This has mainly impact on > overload choosing and parameter passing. Normal assignments using ':=' are > not affected because of the still available implicit type conversion. I came to this when I wanted to do operator overloading on them. The compiler complained that you cannot overload for equal types. So I wanted first to know if it are equal types or not. Next question: Should operator overloading be possible on such types ? Marc _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Christian Iversen
to Peter Vreman: For the compiler the types are not equal anymore, but they are still compatible for…
Peter Vreman wrote 25 lines
> On Saturday 14 October 2006 15:55, Marc Weustink wrote: >> Hi, >> >> if I define 2 types like: >> >> type >> MyA = type string; >> MyB = type string; >> >> are MyA and MyB considered as the same type ? > > No, you are explicitly marking them as a new type. This is a very cool > feature > of Pascal you wont find in many other languages. > > (For instance, you could use it to create a new integer-type for little- > and > big-endian numbers, ensuring that you _never_ directly assign a > little-endian > number to a big-endian one, or vice versa) For the compiler the types are not equal anymore, but they are still compatible for implicit type conversion. This has mainly impact on overload choosing and parameter passing. Normal assignments using ':=' are not affected because of the still available implicit type conversion.

I really feel there should be a way to make two types completely incompatible.

How exactly does the compiler figure out the rules for implicit conversion?

In the case of wrapper integers in records, are 4-byte records handled as efficiently by the compiler as 4-byte integers? I suspect it's some radically different code that handles it.

Regards, Christian Iversen

Reply by email

Peter Vreman
to Christian Iversen
you wrote 34 lines
On Saturday 14 October 2006 17:38, Peter Vreman wrote: > > On Saturday 14 October 2006 15:55, Marc Weustink wrote: > >> Hi, > >> > >> if I define 2 types like: > >> > >> type > >> MyA = type string; > >> MyB = type string; > >> > >> are MyA and MyB considered as the same type ? > > > > No, you are explicitly marking them as a new type. This is a very cool > > feature > > of Pascal you wont find in many other languages. > > > > (For instance, you could use it to create a new integer-type for little- > > and > > big-endian numbers, ensuring that you _never_ directly assign a > > little-endian > > number to a big-endian one, or vice versa) > > For the compiler the types are not equal anymore, but they are still > compatible for implicit type conversion. This has mainly impact on > overload choosing and parameter passing. Normal assignments using ':=' are > not affected because of the still available implicit type conversion. I really feel there should be a way to make two types completely incompatible. How exactly does the compiler figure out the rules for implicit conversion? In the case of wrapper integers in records, are 4-byte records handled as efficiently by the compiler as 4-byte integers? I suspect it's some radically different code that handles it.

Only by making it a record. Conversion of atomic types (string,integer,etc) are always handled internally by the compiler.

Peter

Reply by email

Christian Iversen
to peter green
peter green wrote 6 lines
> In the case of wrapper integers in records, are 4-byte records handled as > efficiently by the compiler as 4-byte integers? I suspect it's > some radically > different code that handles it. test it?

That would require me to actually do something and not just talk about it... ;-)

(but yes, I am going to test it :)

Regards, Christian Iversen

Reply by email