[fpc-pascal] type definitions etc
Vinzent Hoefler
JeLlyFish.software at gmx.net
Thu May 26 14:50:53 CEST 2005
On Thursday 26 May 2005 12:06, Hans Maartensson wrote:
> In a program I had a recursive definition like:
>
> type sometype = record
> n: longint;
> p: ^sometype
> end;
This was never allowed in Pascal, AFAIK. The standard solution is to
define the pointer type first:
|type
| ptr_sometype = ^sometype;
|type
| some_type =
| record
| p : ptr_sometype;
| ...
| end;
> Also the following call to a win32 API function is not allowed any
> more:
>
> invalidaterect(windows, 0, true);
>
> The error is that the 2nd parameter should be of type 'rect'.
Have you actually tried "InvalidateRect (windows, NIL, True);"?
> The 2nd parameter in this example must be of 'var' type, which means
> that it is the pointer to the parameter that is transferred to the
> function. Transferring a nil pointer is in pascal language the same
> as leaving the parameter undefined. But that is not possible in
> FreePascal, is it?
It is, because of function overloading:
function InvalidateRect(hWnd:HWND; var lpRect:RECT;
bErase:WINBOOL):WINBOOL; external 'user32' name 'InvalidateRect';
function InvalidateRect(hWnd:HWND;lpRect:LPRECT;
bErase:WINBOOL):WINBOOL; external 'user32' name 'InvalidateRect';
So the second form takes a pointer, thus passing NIL should be perfectly
allowed, AFAICS.
> Finally I would like to ask about the following.
> The compiler does not allow a procedure definition like:
>
> procedure someproc(p: ^double);
>
> whereas the following is perfectly possible:
>
> type Tpdouble = ^double;
> procedure f(p: Tpdouble);
>
> Is there a good reason for this? Why isn't ^double a valid type
> everywhere?
Because
type1 = ^double;
type2 = ^double;
should be considered different types. So if you define the type in the
procedure declaration, you couldn't call it, just because you can't
declare a variable of the same type for the argument. The only allowed
argument would be NIL, which I think is a little bit pointless.
BTW, why don't you use "procedure f(const/var/out p : double);" instead
to get rid of as much pointers as possible and use the right parameter
modes which also have the side effect of being more self documenting?
Vinzent.
More information about the fpc-pascal
mailing list