### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: how to break out early of a case ?  (Read 43255 times)

#### Warfley

• Hero Member
• Posts: 654
##### Re: how to break out early of a case ?
« Reply #135 on: December 01, 2021, 12:30:46 pm »
But that's my personal opinion.

Code: Text  [Select][+][-]
1. fn main() {
2.     let condition = true;
3.     let number = if condition { 5 } else { 6 };
4.
5.     println!("The value of number is: {}", number);
6. }
7.

MarkMLl
I really like the python version:
Code: Pascal  [Select][+][-]
1. myvar = 6 if condition else 7
By switching the if and value around, you can basically set a focus, the value you put first is so to say the "important" value. Also it allows really great chaining:
Code: Pascal  [Select][+][-]
1. myvar = a if condition1 \
2.    else b if condition2 \
3.    else c if condition3 \
4.    else d
It's (if used with linebreaks) a very clear syntax that is easy to read and understand and also resembles how you would write conditionals in mathematical notation

Compared to for example the condition first syntax:
Code: Pascal  [Select][+][-]
1. myvar = if Condition1 then a
2.    else if condition2 then b
3.    else if condition3 then c
4.    else d;
Here you have always to read the conditions (which might be long expressions themselves) before you get to the value.

It's a question on what a reader might focus, the thing you write first is the thing that the programmer will read and understand at first, while for the second part they might even need to scroll. I think that  it is more important to see what are the possible values, over what are the possible conditions.

Actually, discussions like these can be inspiring. I might just think of supporting a similar shorthand for the compiler I'm working on. But it should be clear and different from other statements, perhaps something like:
Code: Text  [Select][+][-]
1. by condition { number = 5, 6 };

The benefit of such construct is that due to a different keyword (by) it could be processed separately and never reach the expression parser. Just a spontaneous idea.
Personally, just from looking at it, I think this solution isn't very great, because if I didn't have the context on what it is supposed to mean, I wouldn't understand it. Languages should be expressive on their own, reading a program  should be as easy as possible.

Also I think it would be really cluttered if it was chained
« Last Edit: December 01, 2021, 12:41:52 pm by Warfley »

#### MarkMLl

• Hero Member
• Posts: 3654
##### Re: how to break out early of a case ?
« Reply #136 on: December 01, 2021, 01:02:46 pm »
I think the major parsing problem is if the ALGOL-style expression is used (for some crazy reason) when a function result is discarded, e.g.

Code: Pascal  [Select][+][-]
1. { success := } if someSyscall(...) = 0 then true else false
2.

...which will, of course, be rejected by the compiler albeit for the wrong reason. But this is hardly the first time that the compiler's produced not quite the right error message.

MarkMLl
« Last Edit: December 01, 2021, 01:54:15 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### PascalDragon

• Hero Member
• Posts: 3633
• Compiler Developer
##### Re: how to break out early of a case ?
« Reply #137 on: December 01, 2021, 01:43:47 pm »
I don't like flame wars, and from my POV the core team has made their position clear. But it still rankles that a fundamental element that apparently gave ALGOL no problems was omitted from Pascal.

I myself am still a fan of the ifthenelse - expression... Maybe I should start an Oxygene compatibility mode and then allow the use of that expression through a modeswitch.

I also don't like C-style usage of ? since it breaks the otherwise-close correspondence between operators and procedure/function invocation, which in all cases evaluate parameters before applying the operation.

The "not evaluating both branches" is one of the main uses when I use that ternary operator in C(++). It was also the base idea behind the IfThen() intrinsic and would also be the applied to the ifthenelse - expression.

I really like the python version:
Code: Pascal  [Select][+][-]
1. myvar = 6 if condition else 7

I don't like that order as it would require annoying changes in the parser. An initial keyword (in this case if) makes it much easier for the parser to handle (which is also why I'd prefer something like lambda x as x * 2 instead of Oxygene's x => x * 2 for a shorter lambda expression syntax and also why I prefer FPC's specialize keyword).

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #138 on: December 01, 2021, 01:48:09 pm »
Also I think it would be really cluttered if it was chained

You're right about the cluttering when chained, which would basically be sort of a definition switch statement. In SharpBASIC = if implemented = it could be something along the line of:

Code: Text  [Select][+][-]
1. let number on condition is
2.   5 else 6;
3. end;
4.

or chained:

Code: Text  [Select][+][-]
1. let number
2.   on condition1 is
3.     5;
4.   on condition2 is
5.     x else y;
6.   // ...
7.   end;

Food for thought...
« Last Edit: December 02, 2021, 11:03:26 am by munair »
keep it simple

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #139 on: December 01, 2021, 01:51:36 pm »
An initial keyword (in this case if) makes it much easier for the parser to handle

Very true.
keep it simple

#### Warfley

• Hero Member
• Posts: 654
##### Re: how to break out early of a case ?
« Reply #140 on: December 01, 2021, 02:00:46 pm »
I don't like that order as it would require annoying changes in the parser. An initial keyword (in this case if) makes it much easier for the parser to handle (which is also why I'd prefer something like lambda x as x * 2 instead of Oxygene's x => x * 2 for a shorter lambda expression syntax and also why I prefer FPC's specialize keyword).
Completely understandable, but I would still argue that the value then condition structure is better readable, so this could be done with an initial keyword. Just from the top of my head, something like:
Code: Pascal  [Select][+][-]
1. myvar := choose
2.   a if condition1,
3.   b if condition2,
4.   c if condition3,
5.   d otherwise
6. end // probably can be ommited through otherwise terminating the statement
Which is heaviely inspired by the mathematical notation of conditional functions as in
https://i.stack.imgur.com/xDCSl.png
« Last Edit: December 01, 2021, 02:03:14 pm by Warfley »

#### Warfley

• Hero Member
• Posts: 654
##### Re: how to break out early of a case ?
« Reply #141 on: December 01, 2021, 02:07:38 pm »
Code: Text  [Select][+][-]
1. let number by condition is
2.   5 else 6;
3. end;
4.

or chained:

Code: Text  [Select][+][-]
1. let number
2.   by condition1 is
3.     5 else 6;
4.   by condition2 is
5.     x else y;
6.   // ...
7.   end;

Food for thought...
This looks really good, but i would replace "is" with "be" as this sounds much more natural when reading

#### MarkMLl

• Hero Member
• Posts: 3654
##### Re: how to break out early of a case ?
« Reply #142 on: December 01, 2021, 02:12:03 pm »
The "not evaluating both branches" is one of the main uses when I use that ternary operator in C(++). It was also the base idea behind the IfThen() intrinsic and would also be the applied to the ifthenelse - expression.

My argument for spelling it out if-then-else is that it emphasises the control transfer aspect, something I feel an intrinsic IfThen() fails to do.

I did initially have problems accepting the idea because of the way that a number of Object Pascal constructs have Modula-2 explicit end, but have come to believe that it is, ultimately, the cleanest way of doing it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### dseligo

• Sr. Member
• Posts: 440
##### Re: how to break out early of a case ?
« Reply #143 on: December 01, 2021, 02:42:20 pm »
I myself am still a fan of the ifthenelse - expression...

I like this form too. But I think at least if (maybe also then and else) should be changed to something else to avoid confusion and make better distinction between operator and statement.
Maybe: whenreturnotherwise

This is also fine: whenthenelse

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #144 on: December 01, 2021, 02:44:51 pm »
Code: Text  [Select][+][-]
1. let number by condition is
2.   5 else 6;
3. end;
4.

or chained:

Code: Text  [Select][+][-]
1. let number
2.   by condition1 is
3.     5 else 6;
4.   by condition2 is
5.     x else y;
6.   // ...
7.   end;

Food for thought...
This looks really good, but i would replace "is" with "be" as this sounds much more natural when reading

That went also through my mind, but in SharpBASIC there are two types of blocks: is..end for definitions and do..end (like Pascal's begin..end for execution. Additionally, a let statement like this would already introduce two new keywords, which is good enough as it would be referred to as the let..by statement.
keep it simple

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #145 on: December 01, 2021, 02:46:51 pm »
I like this form too. But I think at least if (maybe also then and else) should be changed to something else to avoid confusion and make better distinction between operator and statement.

I expressed the same thought. Let if be if.
keep it simple

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #146 on: December 01, 2021, 04:21:46 pm »
Making the else clause optional one could do

Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is white;
3.   on water == hot is red else blue;
4. end;
keep it simple

#### dseligo

• Sr. Member
• Posts: 440
##### Re: how to break out early of a case ?
« Reply #147 on: December 01, 2021, 05:55:23 pm »
Making the else clause optional one could do

Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is white;
3.   on water == hot is red else blue;
4. end;

And how would you solve conflicts?
E.g.:
Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is white;
3.   on air == hot is red else blue;
4. end;

What would the result be if water is frozen and air is hot?

#### munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
##### Re: how to break out early of a case ?
« Reply #148 on: December 01, 2021, 07:47:24 pm »
Making the else clause optional one could do

Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is white;
3.   on water == hot is red else blue;
4. end;

And how would you solve conflicts?
E.g.:
Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is white;
3.   on air == hot is red else blue;
4. end;

What would the result be if water is frozen and air is hot?

It may look like a conflict, but it really is a definition switch statement:

Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is
3.     white;                   // exits the let statement if true (no implicit fall-through)
4.   on air == hot is
5.     red else blue;           // if no condition is met color = blue
6. end;

So testing is not on 'water' (the conditions) but on color against any condition. I just meant to demonstrate that if the else clause is optional this let construct can be equal to an if statement.
« Last Edit: December 01, 2021, 07:55:26 pm by munair »
keep it simple

#### dseligo

• Sr. Member
• Posts: 440
##### Re: how to break out early of a case ?
« Reply #149 on: December 01, 2021, 09:01:17 pm »
It may look like a conflict, but it really is a definition switch statement:

By 'switch' you mean like switch in C? Case in Pascal?

Quote
Code: Text  [Select][+][-]
1. let color
2.   on water == frozen is
3.     white;                   // exits the let statement if true (no implicit fall-through)
4.   on air == hot is
5.     red else blue;           // if no condition is met color = blue
6. end;

So testing is not on 'water' (the conditions) but on color against any condition. I just meant to demonstrate that if the else clause is optional this let construct can be equal to an if statement.

I think this is confusing. What if we do it like below, what would be value in 'color'?

Code: Text  [Select][+][-]
1. let color
2.   on air == hot is
3.     red else blue;           // if no condition is met color = blue
4.   on water == frozen is
5.     white;                   // exits the let statement if true (no implicit fall-through)
6. end;