Sign in Join

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

Mailing lists / fpc-devel / 2022-08

C library header - var vs pointer for function arguments

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

gabor

I'd like to provide some fixes for packages that are headers for c libraries. When and should I use the "var" argument for function arguments that return a value, or should I always use pointers (excluding arguments that might accept nil)?

Eg:
function Foo(var A: Integer): Integer;
vs
function Foo(A: PInteger): Integer;

Michał.

Reply by email

Dmitry Boyarintsev
to gabor

Consider backwards compatibility in fixes

On Wednesday, August 3, 2022, gabor via fpc-devel <

Show quoted text 17 lines
wrote: I'd like to provide some fixes for packages that are headers for c libraries. When and should I use the "var" argument for function arguments that return a value, or should I always use pointers (excluding arguments that might accept nil)? Eg: function Foo(var A: Integer): Integer; vs function Foo(A: PInteger): Integer; Michał. _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Michael Van Canneyt
to gabor: I'd like to provide some fixes for packages that are headers for c libraries. When and…
gabor wrote 9 lines
I'd like to provide some fixes for packages that are headers for c libraries. When and should I use the "var" argument for function arguments that return a value, or should I always use pointers (excluding arguments that might accept nil)? Eg: function Foo(var A: Integer): Integer; vs function Foo(A: PInteger): Integer;

It's best to use pointers, but you can always provide overloads with var arguments.

And as Dmitry Boyarintsev pointed out, don't change existing signatures for backwards compatibility.

Michael.

Reply by email

Marco van de Voort
to Michael Van Canneyt: It's best to use pointers, but you can always provide overloads with var arguments. And…
Michael Van Canneyt wrote 5 lines
It's best to use pointers, but you can always provide overloads with var arguments.

IIRC it is best to not do this automatically for functions with two such cases (two by ref, so var or pointer parameters).

This because small programming mistakes then often trigger "can't determine which overloaded function to call", which is often confusing for people.

In the Windows headers (rtl/win/wininc and winunits*) I therefore only overload with VAR when it is needed for some compatibility reasons (either FPC self compatibility or Delphi)

Reply by email