Sign in Join

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

Mailing lists / fpc-pascal / 2021-03

Pass type as function parameter

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

Hi,

Would it be possible in FPC to pass a type of a variable as a parameter to a function?

e.g.
  MyFunction(integer);

I have legacy C code that does this via a macro. Hopefully, it can be done so I do not need to change the existing code too badly.

TIA

Rgds, Darius

Reply by email

Michael Van Canneyt
to Darius Blaszyk
Darius Blaszyk wrote 10 lines
Hi, Would it be possible in FPC to pass a type of a variable as a parameter to a function? e.g. MyFunction(integer); I have legacy C code that does this via a macro. Hopefully, it can be done so I do not need to change the existing code too badly.

This is not possible as such.

What you can do is use generics

MyFunction<integer>

or use typeinfo:

MyFunction(TypeInfo(Integer));

Which of the 2 you need to use depends on the code of course.

Michael.

Reply by email

Darius Blaszyk
to Michael Van Canneyt

Thank you so much Michael!

AM Michael Van Canneyt wrote 31 lines
On Wed, 24 Mar 2021, Darius Blaszyk via fpc-pascal wrote: > Hi, > > Would it be possible in FPC to pass a type of a variable as a parameter to > a function? > > e.g. > MyFunction(integer); > > I have legacy C code that does this via a macro. Hopefully, it can be done > so I do not need to change the existing code too badly. This is not possible as such. What you can do is use generics MyFunction<integer> or use typeinfo: MyFunction(TypeInfo(Integer)); Which of the 2 you need to use depends on the code of course. Michael.

Reply by email

Marco van de Voort
to Michael Van Canneyt: This is not possible as such. What you can do is use generics MyFunction<integer> or use…

Op 2021-03-24 om 11:05 schreef Michael Van Canneyt via fpc-pascal:

Show quoted text 24 lines
> Would it be possible in FPC to pass a type of a variable as a > parameter to > a function? > > e.g. >  MyFunction(integer); > > I have legacy C code that does this via a macro. Hopefully, it can be > done > so I do not need to change the existing code too badly. This is not possible as such. What you can do is use generics MyFunction<integer> or use typeinfo: MyFunction(TypeInfo(Integer)); Which of the 2 you need to use depends on the code of course.

Or variants. Or in an emergency: start using/creating an external preprocessor.

Reply by email