Recent

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: how to break out early of a case ?
« Reply #135 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.  :P

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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #136 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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #137 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: 1499
Re: how to break out early of a case ?
« Reply #138 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: 1499
Re: how to break out early of a case ?
« Reply #139 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: 6646
Re: how to break out early of a case ?
« Reply #140 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

  • Hero Member
  • *****
  • Posts: 1177
Re: how to break out early of a case ?
« Reply #141 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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #142 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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #143 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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #144 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

  • Hero Member
  • *****
  • Posts: 1177
Re: how to break out early of a case ?
« Reply #145 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: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #146 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

  • Hero Member
  • *****
  • Posts: 1177
Re: how to break out early of a case ?
« Reply #147 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;

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #148 on: December 01, 2021, 10:53:48 pm »
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;

Color would be blue because the second branch would never be reached. In such case the compiler could give a warning for unreachable code, or the compiler could complain about an else-less branch after an else-branch.
keep it simple

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #149 on: December 01, 2021, 10:56:47 pm »
I just implemented a basic version of the let-statement (only one branch). Works like a charm and I kind'a like it.
keep it simple

 

TinyPortal © 2005-2018