Pointer declarations 
Author Message
 Pointer declarations

Can anyone explain to me why, under TP7.01, the following source
produces a "Type mismatch" error:

  var    x: ^integer;   y: ^integer;
  begin  y := x;  end.

...while the following compiles without an error:

  var    x,y: ^integer;
  begin  y := x;  end.

I note that {$T+} eliminates the error, but the online help on $T is
singularly unhelpful in this case. Declaring

  type intptr = ^integer;

also appears to cure the problem, but why?



Tue, 05 Aug 2003 09:11:27 GMT  
 Pointer declarations

Quote:
> Can anyone explain to me why, under TP7.01, the following source
> produces a "Type mismatch" error:

>   var    x: ^integer;   y: ^integer;
>   begin  y := x;  end.

> ...while the following compiles without an error:

>   var    x,y: ^integer;
>   begin  y := x;  end.

   TP/BP have always had this seemingly trivial weakness: that it can't
recognize that, although you _meant_ the 2 separate declarations to be
identical, the 2 declarations (x & y) are treated as different data
types.
   Annoying, possibly so, but it follows the strict definition of the
Pascal language...and we have to live with it.  A little extra care to
define common types and variable of those types always solves the
problem.
   Note that you'll have the same problem if you do such things with
subprogram parameters...

Quote:
> I note that {$T+} eliminates the error, but the online help on $T is
> singularly unhelpful in this case. Declaring
>   type intptr = ^integer;
> also appears to cure the problem, but why?

   See above.  The compiler now knows the variable are of the same data
type.  


Tue, 05 Aug 2003 11:48:20 GMT  
 Pointer declarations

Quote:

>   TP/BP have always had this seemingly trivial weakness: that it can't
>recognize that, although you _meant_ the 2 separate declarations to be
>identical, the 2 declarations (x & y) are treated as different data
>types.
>   Annoying, possibly so, but it follows the strict definition of the
>Pascal language...and we have to live with it.  A little extra care to
>define common types and variable of those types always solves the
>problem.

Annoying? It is a bonus. Strong typing is t prevent errors. In fact it
should be even stringer. ADA for example has a way to declare new
incompatible types from existing. You could declare a  integer types
which is incompatible with each other.

Osmo



Tue, 05 Aug 2003 14:17:10 GMT  
 Pointer declarations

Quote:


> >   TP/BP have always had this seemingly trivial weakness: that it can't
> >recognize that, although you _meant_ the 2 separate declarations to be
> >identical, the 2 declarations (x & y) are treated as different data
> >types.
> >   Annoying, possibly so, but it follows the strict definition of the
> >Pascal language...and we have to live with it.  A little extra care to
> >define common types and variable of those types always solves the
> >problem.

> Annoying? It is a bonus. Strong typing is t prevent errors. In fact it
> should be even stringer. ADA for example has a way to declare new
> incompatible types from existing. You could declare a  integer types
> which is incompatible with each other.

I think in THIS case it is really annoying. Of course strong typing is
important, but I also cannot understand why

^integer

is not the same as

^integer

.

--
#!/usr/bin/perl
eval($0=q{$0="\neval(\$0=q{$0});\n";for(<*.pl>){open X,">>$_";print X
$0;close X;}print''.reverse"\nsuriv lreP trohs rehtona tsuJ>RH<\n"});
####################### http://learn.to/quote #######################



Wed, 06 Aug 2003 02:20:09 GMT  
 Pointer declarations

Quote:


>I think in THIS case it is really annoying. Of course strong typing is
>important, but I also cannot understand why

>^integer

>is not the same as

>^integer

Where would you draw the line? Should all declarations that look same be
same?

Osmo



Wed, 06 Aug 2003 04:18:03 GMT  
 Pointer declarations

Quote:



>> incompatible types from existing. You could declare a  integer types
>> which is incompatible with each other.

>I think in THIS case it is really annoying. Of course strong typing is
>important, but I also cannot understand why

>^integer

>is not the same as

>^integer

Like e.g. (from my M2 experience)

TYPE
        Handle1type = ^integer;
        Handle2type = ^integer;

Now I use these handles a lot, and make a mistake, and pass a handle for one
handle based subsystem to another. The compiler will detect it, and throw an
error at me, saving me a debugging session.

Moreover, this is really never a big problem to circumvent this, since
sizeof(handle1type)=sizeof(handle2type) makes typecasting possible.

So it saves some faults that could involve heavy debugging, and only costs a
few typestrokes.

Usually, you see the benefits of this only after having done large projects
where you can loose the overview easily. For shorter projects (say till 1000
lines), it only seems waste of time



Wed, 06 Aug 2003 07:01:44 GMT  
 Pointer declarations

Quote:

> Can anyone explain to me why, under TP7.01, the following source
> produces a "Type mismatch" error:

>   var    x: ^integer;   y: ^integer;
>   begin  y := x;  end.

> ...while the following compiles without an error:

>   var    x,y: ^integer;
>   begin  y := x;  end.

> I note that {$T+} eliminates the error, but the online help on $T is
> singularly unhelpful in this case. Declaring

>   type intptr = ^integer;

> also appears to cure the problem, but why?

Hi

^integer is not a type identifier but intptr is. If separate
declarations of variables are made with the same type identifier or
equivalent identifiers they are of equal types, otherwise they are not
equal types, it's part of Borland Pascal and others too. When the
compiler needs to check for type identity it only needs to check
identifiers rather than the equality of a composite list of identifiers.

Regards Hanford



Thu, 07 Aug 2003 01:08:53 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. pointers, pointers, pointers...

2. Pointers Pointers Pointers

3. Variant fields in object declaration in TP7 or DELPHI

4. declaration error

5. Program name declaration

6. ODBC32 API Declaration Unit for Delphi 2.0

7. {10:3 declaration}????

8. write / writeln declaration

9. Help: Unfamiliar type declaration

10. Help: Unfamiliar type declaration

11. Forward declaration Of Data

12. Dynamic array declaration

 

 
Powered by phpBB® Forum Software