Jump to content
Mike Torrettinni

How to switch condition position?

Recommended Posts

19 minutes ago, Mike Torrettinni said:

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 🙂

Don't worry everything is gonna be alright by the time. When I was beginner I asked a similar question : what's the purpose of interface. I got many answers but it took me a lot of time to understand what they were saying.

 

Here is a free advice : first get familiar about the basic of program's life from the loader-time to the cpu-time (just get the basic things) and then master the fundamental stuff of Delphi (also just learn the basic things : statements, functions, array, records, RTL, ...). Now join the open source community and share your first library/app, fork other project and analyse people coding styles (it will be a great experience to you as was mine). after that you will find yourself understanding things quickly even you can master new language with just few days.

Good Luck.  

  • Thanks 1

Share this post


Link to post
13 hours ago, David Heffernan said:

That's certainly more concise but it has a completely different meaning. Think about it Rudy. 

In the case given, it has the same meaning.

I often see people write something like

if condition then 
  Result := True 
else 
  Result := False;

while a reduction like

Result := condition;

is often not considered. 

Share this post


Link to post
On 2/9/2019 at 4:54 PM, Mike Torrettinni said:

 

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');

 

AFAIK, in Python such a construct is used as some kind of ternary operator:

  result = 'N' if (property == 'A') else 'Y'

I guess they didn't put the condition first because then the interpreter will have a harder time infering the type of result (pseudo-Python):

result = if (property == 'A') 'N' else 'Y';

or because there is no nice separation between the condition and the true result.

 

 

Share this post


Link to post
14 hours ago, Uwe Raabe said:

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?

Ok, I get it now, thanks David and Uwe. You guys mean that if Condition is False, Result will not change.

 

Still, then one can do:

Result := Condition or Result;

But this assumes that Condition will be checked first, otherwise a possible function call you expect to be made is not made. I think this order is guaranteed, but I would not really count on it.

 

Anyway, the form with the if clause after the assignment doesn't make sense to me.

Edited by Rudy Velthuis
Typo: üpossible

Share this post


Link to post
15 hours ago, KeithLatham said:

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.

Does Python run on a stack machine? Anyway, if they can manage to have proper if conditions too (and they do) then I don't see why they have this alternative syntax.

 

I looked and saw that CPython emulates a virtual stack machine.

Share this post


Link to post
14 hours ago, David Heffernan said:

That's certainly more concise but it has a completely different meaning. Think about it Rudy. 

Yeah, got it now. It is different if Result was already set before.

Share this post


Link to post
7 hours ago, Rudy Velthuis said:

Does Python run on a stack machine? Anyway, if they can manage to have proper if conditions too (and they do) then I don't see why they have this alternative syntax.

 

I looked and saw that CPython emulates a virtual stack machine.

 Nothing to do with implementation details. The Python syntax is simply the Python equivalent of the C conditional operator. You know, the operator that is missing from Delphi. 

Share this post


Link to post

Rudy said: I think this order is guaranteed, but I would not really count on it.

1 hour ago, David Heffernan said:

There's a compiler switch that overrides this, so it's not really "guaranteed".

 

Share this post


Link to post
1 minute ago, David Schwartz said:

There's a compiler switch that overrides this, so it's not really "guaranteed".

The compiler switch makes it evaluate either all parts, which is perfectly valid in the first place, or evaluates from left to right, which is exactly what we want when writing

Result := Condition or Result;

So whatever that switch is, Condition is evaluated first.

Share this post


Link to post
36 minutes ago, David Schwartz said:

Rudy said: I think this order is guaranteed, but I would not really count on it.

There's a compiler switch that overrides this, so it's not really "guaranteed".

 

No one will switch to complete circuit evaluation. There is a rare case when it makes sense to use it.

function DoSomething(): Boolean;
begin
  // ...
  Result := (not HasError) and DumpStatus();
end;

DumpStatus is a function that will dump the status whether there was an error or not. 

If error exists, DumpStatus will dump the error status. If not it will dump a success status. 

DoSomething's result depends on HasError. So it's important to do a full evaluation of the expression  "(not HasError) and DumpStatus()".

Share this post


Link to post
1 hour ago, David Schwartz said:

Rudy said: I think this order is guaranteed, but I would not really count on it.

There's a compiler switch that overrides this, so it's not really "guaranteed".

 

The point about the order was to be sure that the condition was evaluated. Consider the two settings for the switch:

 

1. Left to right, condition is first sub expression, therefore it is evaluated.

2. Complete eval, all sub expressions are evaluated, therefore it is evaluated.

 

QED

Share this post


Link to post
5 hours ago, David Heffernan said:

I wrote"I think" but was not 100% sure anymore and hadn't looked it up. Your link confirms this:

Quote

Short-circuit evaluation means strict left-to-right evaluation that stops as soon as the result of the entire expression is determined

That is indeed what I thought to remember. It was pretty late though...

Edited by Rudy Velthuis

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×