Recent

Author Topic: Str declaration  (Read 1843 times)

ricardo_sdl

  • New Member
  • *
  • Posts: 23
Str declaration
« on: July 29, 2024, 07:23:13 pm »
Hi!
According to docs, the Str procedure is declared like this:
Code: Pascal  [Select][+][-]
  1. procedure Str(var X: TNumericType[:NumPlaces[:Decimals]];var S: String)
  2.  

Can I declare a procedure like this? If so, how can I use inside the procedure the :NumPlaces and :Decimals values?

Thak you.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #1 on: July 29, 2024, 07:45:56 pm »
No.

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

silvercoder70

  • Jr. Member
  • **
  • Posts: 52
    • Tim Coates
Re: Str declaration
« Reply #2 on: July 30, 2024, 10:15:10 am »
Hi!
According to docs, the Str procedure is declared like this:
Code: Pascal  [Select][+][-]
  1. procedure Str(var X: TNumericType[:NumPlaces[:Decimals]];var S: String)
  2.  

Can I declare a procedure like this? If so, how can I use inside the procedure the :NumPlaces and :Decimals values?

Thak you.


Here is a slightly longer answer...

https://www.freepascal.org/docs-html/rtl/system/str.html
https://wiki.freepascal.org/Str

however, you still cannot. My question would be... what are you trying to achieve?

Tim
P Plate on FPC | YouTube - https://www.youtube.com/@silvercoder70

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #3 on: July 30, 2024, 10:41:22 am »
Historical background.

When Wirth (hurriedly) defined Pascal, it was still common for system operations such as input/output to be embedded in the compiler. IBM had separately-linkable libraries, but they were rather the exception with everybody else using include files; the idea of enforced cross-module parameter checking etc. which implied a uniform procedure/function declaration format was still in the future.

As such, languages including FORTRAN, (some) ALGOLs and (later) C and its derivatives tended to use separate format or picture declarations, with some level of ambiguity as to whether they were parsed at compilation or execution time.

In order to avoid the parsing complexity, particularly where a format declaration was separate from the one or more places where it was used, Wirth took the relatively simple way out: annotate each parameter with minimal information regarding its format. That also made it relatively easy to define that field widths etc. should be computed at runtime, rather than being predefined.

Wirth dropped this notation for Modula-2, and I'm not aware of any Pascal implementation that generalised it to be user-accessible.

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

cdbc

  • Hero Member
  • *****
  • Posts: 1535
    • http://www.cdbc.dk
Re: Str declaration
« Reply #4 on: July 30, 2024, 10:43:00 am »
Hi
As @MarkMLl said, no, 'cause it's an 'Intrinsic' proc in the compiler, a better word for it would be "Magic".  8-)
You can however, emulate this behavior with an overloaded procedure, i.e.: *you* write an overloaded proc, for *every* possible type you'd come across!
Then leave it up to the compiler to pick the right one at runtime  :D
edit: Nice explanation Mark... You were faster.
Regards Benny
« Last Edit: July 30, 2024, 10:47:53 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #5 on: July 30, 2024, 10:47:01 am »
You can however, emulate this behavior with an overloaded procedure, i.e.: *you* write an overloaded proc, for *every* possible type you'd come across!
Then leave it up to the compiler to pick the right one at runtime  :D

But that still wouldn't handle : as the parameter separator.

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

440bx

  • Hero Member
  • *****
  • Posts: 4570
Re: Str declaration
« Reply #6 on: July 30, 2024, 10:47:37 am »
You can however, emulate this behavior with an overloaded procedure, i.e.: *you* write an overloaded proc, for *every* possible type you'd come across!
Then leave it up to the compiler to pick the right one at runtime  :D
That method works reasonably well when parameters are required but when values are optional and worse, separated by semicolons, overloading doesn't "map" that functionality very well.

MarkMLI pointed that out too while I was typing this post.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

cdbc

  • Hero Member
  • *****
  • Posts: 1535
    • http://www.cdbc.dk
Re: Str declaration
« Reply #7 on: July 30, 2024, 10:50:37 am »
Hi
I think it was Mick Jagger, who said "You can't always get what you want"  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ricardo_sdl

  • New Member
  • *
  • Posts: 23
Re: Str declaration
« Reply #8 on: July 30, 2024, 06:40:14 pm »
Thank you all for the answers! I wasn't trying to achieve something, just curiosity about the different procedure declaration and I wondered if I could do the same with my code. Nice history lessons here!

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #9 on: July 30, 2024, 10:28:09 pm »
Looked at obliquely, in some of my own scripting stuff I've experimented with a Smalltalk-based calling notation.

I'd rather not try to dig out any robust examples, because one of my goals was to define the absolute minimum of reserved characters and along the way I painted myself into a corner (and because it was live code decided to live with it). But basically everything was uniform and had parallels with the way FPC initialises record constants: a parameter was named, followed by a colon, and that was followed by a value

Code: [Select]
print(to: stdout value: fred);

or

Code: [Select]
print(to: stdout value: fred format: something);

or potentially

Code: [Select]
print(to: stdout format: something value: fred);


and so on. If a default was to be used then a - was used as a placeholder:

Code: [Select]
print(to-value: fred);

and so on. However there's no easy way to convert this back to usable Pascal without a custom parser.

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #10 on: September 18, 2024, 10:02:45 am »
You can however, emulate this behavior with an overloaded procedure, i.e.: *you* write an overloaded proc, for *every* possible type you'd come across!
Then leave it up to the compiler to pick the right one at runtime  :D

But that still wouldn't handle : as the parameter separator.

Also it appears that since Str() (hence Write() etc.) is "magic" with special handling by the compiler, those functions cannot be overloaded.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 15747
  • Censorship about opinions does not belong here.
Re: Str declaration
« Reply #11 on: September 18, 2024, 01:12:28 pm »
str is a reserved word and a compiler intrinsic. just read all other answers.
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #12 on: September 18, 2024, 01:28:55 pm »
str is a reserved word and a compiler intrinsic. just read all other answers.

I felt it worth making the point in the context of OP, having been casually caught by it yesterday.

However you might care to point it out for me in https://www.freepascal.org/docs-html/current/ref/refse3.html#refsu3.html since I'm blowed if I can see it.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 15747
  • Censorship about opinions does not belong here.
Re: Str declaration
« Reply #13 on: September 18, 2024, 02:09:17 pm »
str belongs to the early pascal family, it is not related to any dialect.
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7725
Re: Str declaration
« Reply #14 on: September 18, 2024, 02:27:26 pm »
str belongs to the early pascal family, it is not related to any dialect.

So do begin, end and so on. Str is not documented as a reserved word.

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

 

TinyPortal © 2005-2018