Recent

Author Topic: [SOLVED] Procedure parameter with no type?  (Read 1072 times)

CM630

  • Hero Member
  • *****
  • Posts: 1224
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] Procedure parameter with no type?
« on: November 25, 2024, 12:49:47 pm »

I have come accross the following routine:
procedure TSomething.AssignFrame(var Buf; Count: integer; bSometihing: boolean; ANr:cardinal; ATime:TDateTime);


I cannot find out what the type of Buf is. The code compiles and runs :o
Could it be a variant or could it be anything?
« Last Edit: November 25, 2024, 02:08:42 pm by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

MarkMLl

  • Hero Member
  • *****
  • Posts: 8141
Re: Procedure parameter with no type?
« Reply #1 on: November 25, 2024, 12:55:57 pm »
Treat it as an untyped pointer. See https://www.freepascal.org/docs-html/current/rtl/system/blockread.html as an example and note that in this situation you will invariably have a count parameter which has to be initialised to the size of the buffer.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories


CM630

  • Hero Member
  • *****
  • Posts: 1224
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Procedure parameter with no type?
« Reply #3 on: November 25, 2024, 02:16:17 pm »
Thanks, I will try to figure these documents out.
Edit: I have handled the case successfully.
« Last Edit: November 26, 2024, 09:00:06 am by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1297
Re: [SOLVED] Procedure parameter with no type?
« Reply #4 on: November 27, 2024, 09:51:56 am »
I used to do things like that so that I could use one procedure for different types of parameters. Are you able to see the inner workings of the procedure with untyped parameter?
I remember having code to test what parameter was and typecast it inside of the procedure...
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

MarkMLl

  • Hero Member
  • *****
  • Posts: 8141
Re: [SOLVED] Procedure parameter with no type?
« Reply #5 on: November 27, 2024, 10:30:45 am »
I used to do things like that so that I could use one procedure for different types of parameters. Are you able to see the inner workings of the procedure with untyped parameter?
I remember having code to test what parameter was and typecast it inside of the procedure...

That sort of thing is best avoided if at all possible, since you're bypassing the type checks that make Pascal worth having.

If the parameter is a descendant of TObject (i.e. an instantiated class) then you can find out what the actual class is, but there's no "what type is this?" functionality in the basic (i.e. pre-OO) language. Even if you are using objects, I don't think you can easily use a case statement.

You can obviously use polymorphic functions, but there you are at the mercy of Pascal's sloppy implicit type conversions (which, as I've said before, Wirth tightened up in later languages). But a better solution is to use variant records where possible, together with careful consideration of where parameters can be declared const to reduce the amount of stuff copied around.

The implementation of procedures with untyped parameters, like the implementation of direct pointer manipulation, should be kept out of the hands of inexperienced programmers or at the very least subject to careful code review and testing.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12006
  • FPC developer.
Re: [SOLVED] Procedure parameter with no type?
« Reply #6 on: November 27, 2024, 11:27:21 am »
The classic examples of untyped variables are the blockread/blockwrite examples that allow to pass records and (static) array directly to it, to read into.

The problem with untyped var is that there is no meta data. You don't get the size that is passed, so there usually is an extra parameter from which to derive size or type

 

TinyPortal © 2005-2018