Recent

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

munair

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 3664
  • Compiler Developer
Re: how to break out early of a case ?
« Reply #152 on: December 02, 2021, 09:34:02 am »
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

The point is that the expression is most likely to be introduced due to an Oxygene language mode. And for that it must be if. So why introduce yet another keyword and syntax?

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #153 on: December 02, 2021, 09:40:26 am »
The point is that the expression is most likely to be introduced due to an Oxygene language mode. And for that it must be if. So why introduce yet another keyword and syntax?

Would there also be a mode directive to enable that individually, or would there be too much risk of incompatibility with other syntax?

Going back to the thread where it was withdrawn, I note that a whole lot of the troublesome cases were actually in variable initialisation or function declaration. Allowing that the whole idea is that this generates conditional code and that variable/parameter initialisation/default is by definition resolved at compilation time, is that usage even valid?

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

munair

  • Hero Member
  • *****
  • Posts: 781
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #154 on: December 02, 2021, 11:09:05 am »
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;

You were right about the confusion in that in my hastiness I initially gave a bad example with multiple else branches. Of course, the very first else would leave the statement. So when chained/switched, only the last branch should be allowed to include else similar to if.
keep it simple

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #155 on: December 02, 2021, 11:17:55 am »
You were right about the confusion in that in my hastiness I initially gave a bad example with multiple else branches. Of course, the very first else would leave the statement. So when chained/switched, only the last branch should be allowed to include else similar to if.

This is not the place to discuss the design of some other language, but from the POV of my experience with multiple ALGOL-based languages- and modern BASIC aspires to that heritage- that syntax... is severely deficient.

The thread was originally about implementing a particular type of control flow using Pascal as provided by FPC, and quite frankly I think we've got quite enough problems of our own around here. I'm also concerned about the consequences if either a Pascal newbie or somebody intent to criticise the language were to read this stuff.

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

munair

  • Hero Member
  • *****
  • Posts: 781
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #156 on: December 02, 2021, 11:47:09 am »
This is not the place to discuss the design of some other language, but from the POV of my experience with multiple ALGOL-based languages-

You yourself chose "with regret" to join this thread with a comment that continued off-topic:

With regret, I join this thread to say I agree. You only have to look at the popular distinction between "curly bracket languages" and "Everything else".
MarkMLl

So why can languages such as C, C++, Algol, Python etc be discussed but not a compiler I'm working on that can benefit from these discussions? I haven't heard the OP or any moderator complaining about the thread so far, on the contrary, and in my experience they're quite lenient about going off-topic.

Quote
and modern BASIC aspires to that heritage- that syntax... is severely deficient.

Programmers tend to be misguided by the reputation that comes with the name BASIC. The SharpBASIC syntax is most definitely NOT deficient (some proposed the language should have another name).

Quote
I'm also concerned about the consequences if either a Pascal newbie or somebody intent to criticise the language were to read this stuff.

Your concern is duly noted.

If you want to discuss a specific language feature as you have in your recent posts undisturbed, then please start your own thread.
« Last Edit: December 02, 2021, 11:54:13 am by munair »
keep it simple

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #157 on: December 02, 2021, 12:09:17 pm »
All fair points. My apologies.

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: 3664
  • Compiler Developer
Re: how to break out early of a case ?
« Reply #158 on: December 02, 2021, 02:01:52 pm »
The point is that the expression is most likely to be introduced due to an Oxygene language mode. And for that it must be if. So why introduce yet another keyword and syntax?

Would there also be a mode directive to enable that individually, or would there be too much risk of incompatibility with other syntax?

Yes and there shouldn't be any risk of incompatibility due to if being a keyword recognized by all language modes.

Going back to the thread where it was withdrawn, I note that a whole lot of the troublesome cases were actually in variable initialisation or function declaration. Allowing that the whole idea is that this generates conditional code and that variable/parameter initialisation/default is by definition resolved at compilation time, is that usage even valid?

I didn't see that the majority is about variable initialization and function declarations. But no, it wouldn't work there (though in theory one could allow it for constant expressions as well for variable initializations).

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #159 on: December 02, 2021, 02:53:40 pm »
Would there also be a mode directive to enable that individually, or would there be too much risk of incompatibility with other syntax?

Yes and there shouldn't be any risk of incompatibility due to if being a keyword recognized by all language modes.

Thanks for that, I've worked through some of the possibilities in the past as a "thought experiment" so didn't think there would be.

Quote
I didn't see that the majority is about variable initialization and function declarations. But no, it wouldn't work there (though in theory one could allow it for constant expressions as well for variable initializations).

There was something in the postmortem... specifically https://lists.freepascal.org/pipermail/fpc-pascal/2016-February/046626.html

Anyway, I should go quiet here since as has been pointed out this isn't "my thread" and I'm responsible for adding noise to 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

440bx

  • Hero Member
  • *****
  • Posts: 2626
Re: how to break out early of a case ?
« Reply #160 on: December 02, 2021, 05:43:19 pm »
Anyway, I should go quiet here since as has been pointed out this isn't "my thread" and I'm responsible for adding noise to it.
I guess I am the thread's "owner".  For the record, I don't mind at all reading discussions of compiler features and possible implementations.  I think it's good for ideas to be publicly discussed and analyzed.

IOW, don't go quiet on my account.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3664
  • Compiler Developer
Re: how to break out early of a case ?
« Reply #161 on: December 03, 2021, 03:51:18 pm »
Quote
I didn't see that the majority is about variable initialization and function declarations. But no, it wouldn't work there (though in theory one could allow it for constant expressions as well for variable initializations).

There was something in the postmortem... specifically https://lists.freepascal.org/pipermail/fpc-pascal/2016-February/046626.html

I take it you mean this?

Code: Pascal  [Select][+][-]
  1. Type
  2.    TMyArray = Array[1..if sizeof(integer)=2 then 4 else 5] of integer;
  3.  
  4.    myconst = if sizeof(integer)=2 then 4 else 5;
  5.  
  6.    Procedure Something(AA : Integer = if sizeof(integer)=2 then 4 else 5);
  7.  
  8.    Property A : Integer Index if sizeof(integer)=2 then 4 else 5 read geta;
  9.  
  10.    Property B : Integer Read FA Write FA default if sizeof(integer)=2 then 4 else 5 ;

Yeah, I agree with MvC here that this is a very good reason not to allow this as a constant expression at least. :-X Though these would look strange with any other construct as well and in fact currently they'd be done by using $if-constructs which makes them even more ugly... :-\

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #162 on: December 03, 2021, 04:31:49 pm »
Yeah, I agree with MvC here that this is a very good reason not to allow this as a constant expression at least. :-X Though these would look strange with any other construct as well and in fact currently they'd be done by using $if-constructs which makes them even more ugly... :-\

I must admit that I'm a bit uneasy mixing directives (i.e. $ifdef something) with symbols declared in the context of the language. Even though being able to do so is extremely useful.

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 3685
Re: how to break out early of a case ?
« Reply #163 on: December 06, 2021, 09:22:29 pm »
Having thought through this a bit more...


I take it you mean this?

Code: Pascal  [Select][+][-]
  1. Type
  2.    TMyArray = Array[1..if sizeof(integer)=2 then 4 else 5] of integer;
  3.  
  4.    myconst = if sizeof(integer)=2 then 4 else 5;
  5.  
  6.    Procedure Something(AA : Integer = if sizeof(integer)=2 then 4 else 5);
  7.  
  8.    Property A : Integer Index if sizeof(integer)=2 then 4 else 5 read geta;
  9.  
  10.    Property B : Integer Read FA Write FA default if sizeof(integer)=2 then 4 else 5 ;

All of those examples could of course be replaced by sizeof(integer) + something... although neither would be robust if at some point an integer became 64 (or more) bits.

However after refactoring a bit of code today where the number of bits needed to represent something was an issue, I find myself wondering whether some other potential cases could be avoided by having something like Log2SizeOf() as an operation which could be resolved at compilation time.

I've flirted with ? and ?? in some of my own scripting stuff, the difference being the behaviour when the conditional evaluated to something other than a Boolean.

However my principal reason for favouring if-then-else as an expression element is to have Pascal implement as much as possible of ALGOL-60: omissions are embarassing.

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

munair

  • Hero Member
  • *****
  • Posts: 781
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: how to break out early of a case ?
« Reply #164 on: December 07, 2021, 11:28:00 am »
However my principal reason for favouring if-then-else as an expression element is to have Pascal implement as much as possible of ALGOL-60: omissions are embarassing.

MarkMLl

Why? ALGOL-60 was an important development at the time and the language was of major influence to later languages. But it also stems from a time when system resources were very limited. So that the then developers chose to allow the IF-statement in expressions came from necessity rather than elegance. Later language developers were wise to drop the construct. Why that would be embarrassing is beyond me.
keep it simple

 

TinyPortal © 2005-2018