Forum > General

functional IF

(1/10) > >>

jamie:

--- Quote from: PascalDragon on November 30, 2021, 11:23:16 pm ---
--- Quote from: marcov on November 29, 2021, 12:50:19 pm ---
--- Quote from: MarkMLl on November 29, 2021, 11:47:34 am ---However as a counterexample I'd like to highlight the ALGOL-style conditional expression that Wirth unaccountably omitted from Pascal:


--- Code: ---  a := if b then c else d;

--- End code ---

since this can't easily be emulated using a function and is such a common idiom in mainstream ALGOL derivatives- by which I of course mean C etc.- that its absence is embarrassing.

--- End quote ---

Hasn't there has been talk about this, ifthen() becoming an intrinsic and only evaluating the sides as necessary.   (because objective pascal needed it???)

I don't know if it happened in the end. Maybe Pascaldragon knows.

--- End quote ---

I had added it back in January 2016 (see announcement mail of which the thread continued into February). After some heated discussions both on the public mailing list as well as the core one I removed it again in February. The addition of the intrinsic is still part of the version history and I think somewhere I still have a patch for the if … then … else - expression (alternatively it would be easily derivable from the provided commit for the IfThen intrinsic).

--- End quote ---

You keep threatening us with that addition!, I am still waiting!

 Show me the code so I can convert these C programs over better since they are loaded with things like that and need to really do some heavy workarounds to avoid unwanted function calls.
 
 Also, This  If then Else etc should work nested wise too!  :D

 I am not asking for must, just show me the CODE!  :P

Jamie

Thaddy:
Regarding the ifthen intrinsic that did not last long, there is now something similar in main because of implicit function specialization. It is also nestable.
[slight mod done later]

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$h+}{$modeswitch implicitfunctionspecialization}{$modeswitch functionreferences}uses   sysutils; type  TMyref = reference to procedure;    procedure test1;  begin    writeln('test1');  end;   procedure test2;  begin    writeln('test2');  end; var   a:integer = 100;b:integer = 50;  c:string = 'test';d:string = 'test';  e:double = 1.0;f:double = 1.0;  g,h:TMyref;begin  g := test1;  h := test2;  ifthen(true,g,h)();  ifthen(false,g,h)();  writeln(ifthen(b < a,b,a));                  writeln(ifthen(c = d,'equal','not equal'));  c := extract(d);  writeln(ifthen(c = d,'equal','not equal'));  writeln(ifthen(e = f,e,f):2:2);end.

jamie:
@thaddy
  Not the same and never will be unti the operation is intrinsic to avoid executing unwanted code.

Thaddy:
I know, but it is close and has very little speed penalty due to the nature of generics.

Warfley:
Was bored and wanted to see how long it takes to build something like that:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  s: string;begin  s := if 0 < 1 then 'Foo' else 'Bar';  WriteLn(s);end.
It's roughly 30 lines of code and took 15 minutes to create the attached patch for fpc main

Navigation

[0] Message Index

[#] Next page

Go to full version