Recent

Author Topic: Change execution order when a routine is called  (Read 301 times)

jollytall

  • Sr. Member
  • ****
  • Posts: 333
Change execution order when a routine is called
« on: September 12, 2024, 02:42:37 pm »
I have a rather simple case. My input is an s string, what I slice from left to write. Every time CutFirst is called, it gives back as a return value a string AND it updates the input to be shorter (removing the first element cutting the string at a certain separator). This works perfectly.

Now I want to call a routine with the cut down parts:
Foo(CutFirst(s), CutFirst(s), CutFirst(s));

My common sense would say that in Foo the tree attributes are the three elements from left to right, but it is the opposite. I guess it is because, unless really needed, the attributes are evaluated right to left (it is a bit unclear to me why this is the standard). In this particular case it is really needed to make the evaluation of the functions in the attributes left to right, but the compiler does not notice that it is important this case.

How can I manually force it? (I have many more than three parameters, and so I want to avoid putting the cut pieces into variables first before calling Foo. Also because Foo might be called from elsewhere I do not want to change the order of the parameters either.)

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: Change execution order when a routine is called
« Reply #1 on: September 12, 2024, 02:52:20 pm »
https://wiki.freepascal.org/Code_Conversion_Guide#Order_of_parameter_evaluation
Quote
Order of parameter evaluation

Delphi guarantees that all parameters are evaluated from left to right. FPC makes no such guarantee, and can evaluate parameters in any order it wants in order to generate optimal code.

Mode Delphi?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Bart

  • Hero Member
  • *****
  • Posts: 5364
    • Bart en Mariska's Webstek
Re: Change execution order when a routine is called
« Reply #2 on: September 12, 2024, 02:53:35 pm »
I don't think you can.
The wiki states that the order of parameter evaluation in fpc is not guaranteed:
Quote
Delphi guarantees that all parameters are evaluated from left to right. FPC makes no such guarantee, and can evaluate parameters in any order it wants in order to generate optimal code.

You could overload foo to achieve what you want.
If you already have an overload with 1 string param, add a DoCutFurst: Boolean parameter, then do consecutive cuts and call the foo(S1, S2, S3) variant.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6579
Re: Change execution order when a routine is called
« Reply #3 on: September 12, 2024, 02:59:55 pm »
A function with var array of string as the parameter
The only true wisdom is knowing you know nothing

Khrys

  • Jr. Member
  • **
  • Posts: 96
Re: Change execution order when a routine is called
« Reply #4 on: September 12, 2024, 03:02:06 pm »
I'm afraid this is not possible; FPC does not guarantee any specific order of parameter evaluation, unlike Delphi. Recently there was a similiar discussion on this forum.

Since it seems that you're just trying to extract some delimiter-separated substrings, I'd suggest making  CutFirst  a pure function and have it return a string array, while also modifying  Foo  to accept such an array:

Code: Pascal  [Select][+][-]
  1. function CutFirstN(var Subject: String; Count: Cardinal): TStringArray;
  2.  
  3. procedure Foo(Parts: TStringArray);
  4.  
  5. Foo(CutFirstN(s, 3));

 

TinyPortal © 2005-2018