Recent

Author Topic: IfThenStr does not work - omg  (Read 1683 times)

jamie

  • Hero Member
  • *****
  • Posts: 6867
Re: IfThenStr does not work - omg
« Reply #15 on: March 16, 2025, 07:09:16 pm »
which raises the question from my end, since I've been out of commission for some months limiting my time here.

Has the compiler introduced a real Iften function ?, you know one that only evaluates the results of the answer not before hand>

for example:
Code: Pascal  [Select][+][-]
  1. SomeNeededResults := If theCondition Then TrueAnswerFunction else falseAnswerFunciton;
  2.  

Jamie

The only true wisdom is knowing you know nothing

Zoran

  • Hero Member
  • *****
  • Posts: 1918
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: IfThenStr does not work - omg
« Reply #16 on: March 16, 2025, 07:20:50 pm »
which raises the question from my end, since I've been out of commission for some months limiting my time here.

Has the compiler introduced a real Iften function ?, you know one that only evaluates the results of the answer not before hand>

for example:
Code: Pascal  [Select][+][-]
  1. SomeNeededResults := If theCondition Then TrueAnswerFunction else falseAnswerFunciton;
  2.  

Jamie

Unfortunately not. PascalDragon implemented it long time ago as an intrinsic function, but it was rejected by other developers.
See: https://lists.freepascal.org/pipermail/fpc-pascal/2016-January/046376.html
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

jamie

  • Hero Member
  • *****
  • Posts: 6867
Re: IfThenStr does not work - omg
« Reply #17 on: March 16, 2025, 07:35:02 pm »
IF that is the case, then I believe FP needs to put it back in.

There is already ifThen in the libs, but they evaluate the resulting functions before the condition is known, since it is a function.

Proper handling of this code would allow only the function needed to get executed which solves two problems, unwanted call to the other function
and execution speed.

 Using the Case state does this but it does not return a results.

 this of course, like you say would be an intrinsic operation.

 I invision the days where I can use this code within prototype calls as functions are called on the fly..

Code: Pascal  [Select][+][-]
  1.  
  2.  SomeFunction (If Condition then TrueFunction else FalseFunction);
  3.  
  4.  
  5.  

 oh well.

Jamie

The only true wisdom is knowing you know nothing

paule32

  • Sr. Member
  • ****
  • Posts: 376
Re: IfThenStr does not work - omg
« Reply #18 on: March 16, 2025, 08:19:03 pm »
you coming from C ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

jamie

  • Hero Member
  • *****
  • Posts: 6867
Re: IfThenStr does not work - omg
« Reply #19 on: March 16, 2025, 08:44:55 pm »
you coming from C ?

What gave it away? :o

jamie
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 579
Re: IfThenStr does not work - omg
« Reply #20 on: March 16, 2025, 08:45:22 pm »
Quote

Unfortunately not. PascalDragon implemented it long time ago as an intrinsic function, but it was rejected by other developers

Maybe it is time to renew the feature request? :-[

CM630

  • Hero Member
  • *****
  • Posts: 1310
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: IfThenStr does not work - omg
« Reply #21 on: March 16, 2025, 09:33:23 pm »
 
Code: Pascal  [Select][+][-]
  1. ShowMessage(ifthen(true,'true','false'));
results in
 
Quote
Error: Generics without specialization cannot be used as a type for a variable
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Zoran

  • Hero Member
  • *****
  • Posts: 1918
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: IfThenStr does not work - omg
« Reply #22 on: March 17, 2025, 12:54:58 am »
 
Code: Pascal  [Select][+][-]
  1. ShowMessage(ifthen(true,'true','false'));
results in
 
Quote
Error: Generics without specialization cannot be used as a type for a variable

There will be Implicit specialization in next major version of fpc, which might arrive even in our grandchildren's lives.
« Last Edit: March 17, 2025, 12:56:59 am by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

ALLIGATOR

  • Full Member
  • ***
  • Posts: 148
Re: IfThenStr does not work - omg
« Reply #23 on: March 17, 2025, 04:12:13 am »
...which might arrive even in our grandchildren's lives.
😅➡️😟

MarkMLl

  • Hero Member
  • *****
  • Posts: 8355
Re: IfThenStr does not work - omg
« Reply #24 on: March 17, 2025, 08:30:27 am »
I agree with Jamie.

you coming from C ?

No, ALGOL-60. Wirth omitted it when he developed Pascal, which was (for political reasons) a rush job.

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: 16763
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #25 on: March 17, 2025, 10:28:07 am »
Yes, my patch does indeed work with implicit specialization, but note that the implicit specialization feature is not always stable yet. There are quite some caveats in current main and it is quite hard to debug what's going wrong.
This works:
Code: Pascal  [Select][+][-]
  1. {$if fpc_fullversion < 30301}{$error this code needs 3.3.1 or higher}{$ifend}
  2. {$mode delphi}{$modeswitch implicitfunctionspecialization}
  3. uses sysutils;
  4. type
  5.   Ttest = function:string;
  6.  
  7. function SomeTestOne:string;
  8. begin
  9.  writestr(result,'Test1');
  10. end;
  11.  
  12. function SomeTestTwo:string;
  13. begin
  14.  writestr(result,'Test2')
  15. end;  
  16.  
  17. var
  18.   a:integer = 100;
  19.   b:integer = 200;
  20.   c:single = 0.001;
  21.   d:single = 1.000;
  22.   e:TTest = SomeTestOne;
  23.   f:TTest = SomeTestTwo;
  24. begin
  25.   // all these use sysutils.ifthen<T>
  26.   // using implicit specialization
  27.   writeln(ifthen(true,a,b));
  28.   writeln(ifthen(false,a,b));
  29.   writeln(ifthen(true,c,d):2:3);
  30.   writeln(ifthen(false,c,d):2:3);
  31.   writeln(ifthen(true,e,f));  // executes e
  32.   writeln(ifthen(false,e,f)); // executes f
  33.   writeln(ifthen(true,'true','false'));
  34.   writeln(ifthen(false,'true','false'));  
  35. end.
But some scenario's can lead to known problems, merely related to scoping and parameter sizes.
E.g. if math and/or strutils are also used.
A solution is to include ifthen<T> in system instead, but in that case PascalDragon's intrinsic would be a much better solution than a generic.
« Last Edit: March 17, 2025, 10:42:19 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Thaddy

  • Hero Member
  • *****
  • Posts: 16763
  • Ceterum censeo Trump esse delendam
Re: IfThenStr does not work - omg
« Reply #26 on: March 17, 2025, 10:51:48 am »
Some pitfalls are discussed here:
https://forum.lazarus.freepascal.org/index.php?topic=59124.0
Few have been fixed yet.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

 

TinyPortal © 2005-2018