Mike Torrettinni 198 Posted February 9, 2019 Is it possible to switch the condition to the end: from this: If (condition) Then Result := True; into something like this: Result := True if (condition); Or similar variation, where result is shown first. I know that Case works like that, but I need it to work on string values. When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference. Share this post Link to post
Leif Uneus 43 Posted February 9, 2019 Result := Result or (condition); 1 Share this post Link to post
David Heffernan 2345 Posted February 9, 2019 1 hour ago, Mike Torrettinni said: Is it possible to switch the condition to the end: from this: If (condition) Then Result := True; into something like this: Result := True if (condition); Or similar variation, where result is shown first. I know that Case works like that, but I need it to work on string values. When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference. What does the documentation say? Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 1 hour ago, Leif Uneus said: Result := Result or (condition); I didn't provide good example, I'm trying to work on statement like this, working on strings: if Property = 'A' then Result := 'N'; so looking for in idea how to have something like this: Result := 'N' if/when (Property = 'A'); Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 47 minutes ago, David Heffernan said: What does the documentation say? Well, I can't see anything helpful for my case. I'm looking here: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declarations_and_Statements_(Delphi) Share this post Link to post
dummzeuch 1505 Posted February 9, 2019 No. You might want to look at Perl for that. 😉 1 Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 Thanks. I just had an idea if this is possible for one of my statement, where it's a long condition and wanted to reverse so I can see result first. Share this post Link to post
Uwe Raabe 2057 Posted February 9, 2019 Write your own procedure: procedure MPC(var Result: string; const Value: string; Condition: Boolean); // MPC stands for Mike's Preferenced Condition begin if Condition then Result := Value; end; ... // call MPC(Result, 'N', Property = 'A'); You can even make it a function, but then you must provide a value for the false condition, too. 1 Share this post Link to post
David Schwartz 426 Posted February 9, 2019 (edited) There's always the IfThen function. Result := IfThen( aStr = 'OK', True, False ); The problem is, this form If (condition) Then Result := True; does not set Result if condition is false. So it can be undefined or random. Edited February 9, 2019 by David Schwartz Share this post Link to post
David Heffernan 2345 Posted February 9, 2019 3 hours ago, Mike Torrettinni said: Well, I can't see anything helpful for my case. I'm looking here: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declarations_and_Statements_(Delphi) So you know the answer Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 47 minutes ago, David Heffernan said: So you know the answer Yes, no matter what i tried, it didn't compile. Was hoping there is simple solution, missing from help. No luck. Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 1 hour ago, Uwe Raabe said: Write your own procedure: procedure MPC(var Result: string; const Value: string; Condition: Boolean); // MPC stands for Mike's Preferenced Condition begin if Condition then Result := Value; end; ... // call MPC(Result, 'N', Property = 'A'); You can even make it a function, but then you must provide a value for the false condition, too. Thank you, but was trying to avoid using methods, it kind of defeats the purpose of simple If statement. Share this post Link to post
Mike Torrettinni 198 Posted February 9, 2019 1 hour ago, David Schwartz said: There's always the IfThen function. Result := IfThen( aStr = 'OK', True, False ); The problem is, this form If (condition) Then Result := True; does not set Result if condition is false. So it can be undefined or random. IfThen's are awesome, I use them a lot, but it's not usable for this case, I need Result to be very clear at the beginning. Share this post Link to post
Rudy Velthuis 91 Posted February 10, 2019 (edited) 14 hours ago, Mike Torrettinni said: Is it possible to switch the condition to the end: from this: If (condition) Then Result := True; into something like this: Result := True if (condition); No, and I am glad about that. having the if after the condition is something I never understood or liked in Python. I mean I understand the construct but I never understood why it was done that way. And I know one must be Dutch to understand some things in Python, but heck, I am Dutch, and have been for the last 58 years (I'm only 4 years younger than Van Rossum). FWIW, the parentheses are not required here: if Condition then Result := True; Or even better: Result := Condition; Edited February 10, 2019 by Rudy Velthuis Share this post Link to post
KeithLatham 5 Posted February 10, 2019 3 hours ago, Rudy Velthuis said: No, and I am glad about that. having the if after the condition is something I never understood or liked in Python. I mean I understand the construct but I never understood why it was done that way. And I know one must be Dutch to understand some things in Python, but heck, I am Dutch, and have been for the last 58 years (I'm only 4 years younger than Van Rossum). FWIW, the parentheses are not required here: if Condition then Result := True; Or even better: Result := Condition; I am glad too. But to address the part about you not understanding the construct, I would suggest that it resembles 'reverse Polish notation', and that is notoriously easy to implement in a stack environment. I have an book on interpreters somewhere in the bowels of my archeology site that some would laughingly call a 'library' that discusses exactly that method for implementing a Fortran interpreter. Share this post Link to post
KeithLatham 5 Posted February 10, 2019 (edited) 18 hours ago, Mike Torrettinni said: Is it possible to switch the condition to the end: from this: If (condition) Then Result := True; into something like this: Result := True if (condition); Or similar variation, where result is shown first. I know that Case works like that, but I need it to work on string values. When visually looking at the statement, sometimes the Result value has a 'priority' over condition - my personal preference. Huh, yeah like Rudy said, how about Result := (condition) So your example if Property = 'A' then Result := 'N'; becomes Result := { start anonymous method } function(old, test, new: string) : string begin Result := old; if old := test then result := new; end; { end anonymous method } NO. anonymous method bad (IMHO) Much better is: function tester(old, test, new: string) : string; begin Result := old; if old := test then result := new; end; and then call it like function domything : string; begin {do something to establish the initial value of result} Result := tester(result, 'A', 'N'); end; Fundamentally, you are trying to do something that just is not in the syntax of the language. The designer(s) of the Pascal language made syntax choices that give the language its 'flavor'. Other designers gave other languages different syntax because it appealed to them or they saw 'easy' ways to implement them or whatever. It is pointless to choose an implementation language and then bang against the design choices inherent in that language. If you are used to something working one way in one language that you don't see in Pascal, then you need to understand that you are now working in Pascal, not in that other language. Your goal is not to write a program that 'looks like a similar program in language X'; your goal is to solve a problem in the language you're are working with. EVERY language whether it is Pascal, Perl, Python, Lisp, Cobol, APL, Fortran, Simula, Visual Basic, or anything else has constructs to accomplish your goal. But they all look differently when they do it, and that is what makes them different from one another. Edited February 10, 2019 by KeithLatham spelling 1 Share this post Link to post
David Heffernan 2345 Posted February 10, 2019 12 hours ago, Mike Torrettinni said: Yes, no matter what i tried, it didn't compile. Was hoping there is simple solution, missing from help. No luck. Seems kind of a waste of time this entire discussion. The language is well documented and is quite concise. The are only a handful of conditional statements. The syntax for if is clear. You have to write the code in the language that is provided to you. Use an if statement. End of story. 1 Share this post Link to post
David Heffernan 2345 Posted February 10, 2019 4 hours ago, Rudy Velthuis said: Or even better: Result := Condition; That's certainly more concise but it has a completely different meaning. Think about it Rudy. Share this post Link to post
Uwe Raabe 2057 Posted February 10, 2019 if Condition then Result := True; Result := Condition; These are not the same! What if Result is true before that line in the first and in the second case? 1 Share this post Link to post
Mahdi Safsafi 225 Posted February 10, 2019 15 hours ago, Uwe Raabe said: Write your own procedure: procedure MPC(var Result: string; const Value: string; Condition: Boolean); // MPC stands for Mike's Preferenced Condition begin if Condition then Result := Value; end; ... // call MPC(Result, 'N', Property = 'A'); You can even make it a function, but then you must provide a value for the false condition, too. You might consider to inline it ... the call is too expensive ! Also what about that ? Result := Condition or Result; Share this post Link to post
Uwe Raabe 2057 Posted February 10, 2019 36 minutes ago, Mahdi Safsafi said: Also what about that ? Result := Condition or Result; That is a valid approach for the Result := Condition part, but in the original example Result is a string while Condition is a Boolean; Share this post Link to post
Mahdi Safsafi 225 Posted February 10, 2019 49 minutes ago, Uwe Raabe said: That is a valid approach for the Result := Condition part, but in the original example Result is a string while Condition is a Boolean; Ah I see ! If I know that the compiler can do a good job about optimisation I'd write something like this: begin // ... // Requires Rio const table = [Result, 'true']; Result := table[integer(Condition)]; end; desperately waiting for : Result := Condition ? Value : Result; Share this post Link to post
Mike Torrettinni 198 Posted February 10, 2019 While I appreciate all the engagement in this thread, the whole question was just a simple 'is it possible' idea. A little more explanation when I got this idea (please consider my beginners level of Delphi experience): So, I have a function that returns default value of a property and since I was just building up this feature, I wanted to know what the defaults are right away. So, this is rough example: if MatchStr('PropertyName', ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7', 'Prop8', 'Prop9', 'Prop10' ]) then Result := 'N' else if (MatchStr('PropertyName', ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD', 'PropertyE', 'PropertyF', 'PropertyG', 'PropertyH' 'PropertyI', 'PropertyJ', 'PropertyK', 'PropertyL', 'PropertyM', 'PropertyN', 'PropertyO', 'PropertyP', 'PropertyQ'...]) then Result := 'DefaultABC' else if PropertyName = 'CustomProp' Result := 'X' ... and since I was adding new property names and value, I always wanted to know exactly what is already there and what the names and values are, and a Result was an important information, so I was thinking it would be so cool to have it reversed: Result := 'N' if MatchStr('PropertyName', ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7', 'Prop8', 'Prop9', 'Prop10' ]) else Result := 'DefaultABC' if MatchStr('PropertyName', ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD', 'PropertyE', 'PropertyF', 'PropertyG', 'PropertyH' 'PropertyI', 'PropertyJ', 'PropertyK', 'PropertyL', 'PropertyM', 'PropertyN', 'PropertyO', 'PropertyP', 'PropertyQ'...]) else Result := 'X' if (PropertyName = 'CustomProp') .... So, it's more visual thing, where I first see possible default values and then which property names resolve to it. I know I can do this 10s of other way with lookup table, consts... but this all means the default values and property names are not together, and I would need to jump back and forth to get the right information. Share this post Link to post
Mahdi Safsafi 225 Posted February 10, 2019 Quote While I appreciate all the engagement in this thread, the whole question was just a simple 'is it possible' idea. Well Mike. This is our community. They're all exited and each topic turns just like this one xD ... All people here love coding ... and I'm sure you will love this community too. You said your're a new to Delphi. We welcome you here 🙂. Now for your question about using "Result := Value if(Condition);" , the sort answer is not possible. I believe that using table lookup is match easy for you: type TRec = record Patterns: array of string; Result: string; end; PRec = ^TRec; const Table: array [0 .. 1] of TRec = ( (Patterns: ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5', 'Prop6', 'Prop7']; Result: 'N'), (Patterns: ['PropertyA', 'PropertyB', 'PropertyC', 'PropertyD']; Result: 'DefaultABC') ); function LookUp(const AProperty: string): string; var I: Integer; Rec: PRec; begin for I := 0 to Length(Table) - 1 do begin Rec := @Table[I]; if (MatchStr(AProperty, Rec^.Patterns)) then exit(Rec^.Result); end; Result := 'Default'; end; Because you're a new to Delphi, I believe that's the best way for you right now. Later you can optimise it by using binary string search, hash-code, magic-index, crc32 and binary lookup, ... 1 Share this post Link to post
Mike Torrettinni 198 Posted February 10, 2019 3 minutes ago, Mahdi Safsafi said: You said your're a new to Delphi. We welcome you here 🙂. Thank you for the example! I'm not really new to Delphi, but never grew out of the beginners's level of thinking, I guess... you can find a thread here me questioning about why should I use classes at all 🙂 Share this post Link to post