Re: else ladders practice

Liste des GroupesRevenir à cl c  
Sujet : Re: else ladders practice
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 04. Nov 2024, 20:50:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vgb8if$13ioj$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
User-Agent : Mozilla Thunderbird
On 04/11/2024 16:35, David Brown wrote:
On 03/11/2024 21:00, Bart wrote:

To my mind, this is a type of "multi-way selection" :
      (const int []){ a, b, c }[n];
 I can't see any good reason to exclude it as fitting the descriptive phrase.

And if "a", "b" and "c" are not constant, but require evaluation of some sort, it does not change things.  Of course if these required significant effort to evaluate,
Or you had a hundred of them.

or had side-effects, then you would most likely want a "multi-way selection" construction that did the selection first, then the evaluation - but that's a matter of programmer choice, and does not change the terms.
You still don't get how different the concepts are. Everybody is familiar with N-way selection when it involves actions, eg. statements. Because they will be in form of a switch statement, or an if-else chain.
They will expect one branch only to evaluated. Otherwise, there's no point in a selection or condition, if all will be evaluated anyway!
But I think they are less familiar with the concept when it mainly involves expressions, and the whole thing returns a value.
The only such things in C are the ?: operator, and those compound literals. And even then, those do not allow arbitrary statements.
Here is a summary of C vs my language.
In C, 0 or 1 branches will be evaluated (except for ?: where it is always 1.)
In M, 0 or 1 branches are evaluated, unless it yields a value or lvalue, then it must be 1 (and those need an 'else' branch):
                                         C     M
if-else branches can be exprs/stmts     Y     Y
if-else can yield a value               N     Y
if-else can be an lvalue                N     Y
?: branches can be exprs/stmts          Y     Y   (M's is a form of if)
?: can yield a value                    Y     Y
?: can be an lvalue                     N     Y   (Only possible in C++)
switch branches can have expr/stmts     Y     Y
switch can yield a value                N     Y
switch can be an lvalue                 N     Y
select can have exprs/stmts             -     Y   (Does not exist in C)
select can yield a value                -     Y
select can be an lvalue                 -     Y
case-select has exprs/stmts             -     Y
case-select can yield a value           -     Y
case-select can be an lvalue            -     Y
15 Ys in the M column, vs 4 Ys in the C column, with only 1 for value-returning. You can see why C users might be less familiar with the concepts.

I am very keen on keeping the concepts distinct in cases where it matters.
I know, you like to mix things up. I like clear lines:
   func F:int ...              Always returns a value
   proc P  ...                 Never returns a value

    int x = if (randomfloat()<0.5) 42;
>
 In C, no.  But when we have spread to other languages, including hypothetical languages, there's nothing to stop that.  Not only could it be supported by the run-time type system, but it would be possible to have compile-time types that are more flexible
This is a program from my 1990s scripting language which was part of my CAD application:
    n := 999
    x := (n | 10, 20, 30)
    println x
This uses N-way select (and evaluating only one option!). But there is no default path (it is added by the bytecode compiler).
The output, since n is out of range, is this:
    <Void>
In most arithmetic, using a void value is an error, so it's likely to soon go wrong. I now require a default branch, as that is safer.

and only need to be "solidified" during code generation.  That might allow the language to track things like "uninitialised" or "no value" during compilation without having them part of a real type (such as std::optional<> or a C
But you are always returning an actual type in agreement with the language. That is my point. You're not choosing to just fall off that cliff and return garbage or just crash.
However, your example with std::optional did just that, despite having that type available.

It doesn't return a value.  That is why it is UB to try to use that non-existent value.
And why it is so easy to avoid that UB.

My language will not allow it. Most people would say that that's a good thing. You seem to want to take the perverse view that such code should be allowed to return garbage values or have undefined behaviour.
 Is your idea of "most people" based on a survey of more than one person?
So, you're suggesting that "most people" would prefer a language that lets you do crazy, unsafe things for no good reason? That is, unless you prefer to fall off that cliff I keep talking about.
The fact is, I spend a lot of time implementing this stuff, but I wouldn't even know how to express some of the odd things in C. My current C compiler uses a stack-based IL. Given this:
   #include <stdio.h>
   int F(void){}
   int main(void) {
       int a;
       a=F();
       printf("%d\n", a);
   }
It just about works when generating native code (I'm not quite sure how); but it just returns whatever garbage is in the register:
   c:\cxp>cc -run t        # here runs t.c as native code in memory
   1900545
But the new compiler can also directly interpret that stack IL:
   c:\cxp>cc -runp t
   PC Exec error: RETF/SP mismatch: old=3 curr=2   seqno: 7
The problem is that the call-function handling expects a return value to have been pushed. But nothing has been pushed in this case. And the language doesn't allow me to detect that.
(My compiler could detect some cases, but not all, and even it it could, it would report false positives of a missing return, for functions that did always return early.)
So this is a discontinuity in the language, a schism, an exception that shouldn't be there. It's unnatural. It looked off to me, and it is off in practice, so it's not just an opinion.
To fix this would require my always pushing some dummy value at the closing } of the function, if the operand stack is empty at that point.
Which is sort of what you are claiming you don't want to impose on the programmer. But it looks like it's needed anyway, otherwise the function is out of kilter.

Note that I have not suggested returning garbage values - I have suggested that a language might support handling "no value" in a convenient and safe manner.
But in C it is garbage. And I've show an example of my language handling 'no value' in a scheme from the 1990s; I decided to require an explicit 'else' branch, which you seem to think is some kind of imposition.
Well, it won't kill you, and it can make programs more failsafe. It is also friendly to compilers that aren't 100MB monsters.

Totally independent of and orthogonal to that, I strongly believe that there is no point in trying to define behaviour for something that cannot happen,
But it could for n==4.

With justification. 0010 means 8 in C? Jesus.
>
 I think the word "neighbour" is counter-intuitive to spell.
EVERYBODY agrees that leading zero octals in C were a terrible idea. You can't say it's just me thinks that!

Once a thread here has wandered this far off-topic, it is perhaps not unreasonable to draw comparisons with your one-man language.
Suppose I'd made my own hammer. The things I'd use it for are not going to that different: hammering in nails, pulling them out, or generally bashing things about.
As I said, the things my language does are universal. The way it does them are better thought out and tidier.

The real problem with your language is that you think it is perfect
Compared with C, it a huge improvement. Compared with most other modern languages, 95% of what people expect now is missing.

    int F() {
        F(1, 2.3, "four", F,F,F,F(),F(F()));
        F(42);

It is undefined behaviour in C.  Programmers are expected to write sensible code.
But it would be nice if the language stopped people writing such things, yes?
Can you tell me which other current languages, other than C++ and assembly, allow such nonsense?
None? So it's not just me and my language then! Mine is lower level and still plenty unsafe, but it has somewhat higher standards.

If I were the designer of the C language and the maintainer of the C standards, you might have a point.  C is not /my/ language.
You do like to defend it though.

We can agree that C /lets/ people write messy code.  It does not /require/ it.  And I have never found a programming language that stops people writing messy code.
I had included half a dozen points that made C's 'if' error prone and confusing, that would not occur in my syntax because it is better designed.
You seem to be incapable of drawing a line beween what a language can enforce, and what a programmer is free to express.
Or rather, because a programmer has so much freedom anyway, let's not bother with any lines at all! Just have a language that simply doesn't care.

Date Sujet#  Auteur
31 Oct 24 * else ladders practice255fir
31 Oct 24 +* Re: else ladders practice9Anton Shepelev
31 Oct 24 i+- Re: else ladders practice1fir
31 Oct 24 i`* Re: else ladders practice7James Kuyper
1 Nov 24 i `* Re: else ladders practice6David Brown
2 Nov 24 i  +* Re: else ladders practice2James Kuyper
2 Nov 24 i  i`- Re: else ladders practice1David Brown
2 Nov 24 i  `* Re: else ladders practice3fir
2 Nov 24 i   +- Re: else ladders practice1David Brown
2 Nov 24 i   `- Re: else ladders practice1James Kuyper
31 Oct 24 +* Re: else ladders practice5Richard Harnden
31 Oct 24 i+* Re: else ladders practice3fir
31 Oct 24 ii`* Re: else ladders practice2fir
31 Oct 24 ii `- Re: else ladders practice1fir
31 Oct 24 i`- Re: else ladders practice1Bonita Montero
31 Oct 24 +* Re: else ladders practice22Dan Purgert
31 Oct 24 i+* Re: else ladders practice3fir
31 Oct 24 ii`* Re: else ladders practice2Dan Purgert
31 Oct 24 ii `- Re: else ladders practice1fir
16 Nov 24 i`* Re: else ladders practice18Stefan Ram
16 Nov 24 i +* Re: else ladders practice5Bart
16 Nov 24 i i`* Re: else ladders practice4David Brown
19 Nov 24 i i `* Re: else ladders practice3Janis Papanagnou
19 Nov 24 i i  +- Re: else ladders practice1David Brown
19 Nov 24 i i  `- Re: else ladders practice1Michael S
16 Nov 24 i +* Re: else ladders practice3James Kuyper
19 Nov 24 i i`* Re: else ladders practice2Janis Papanagnou
1 Dec 24 i i `- Re: else ladders practice1Tim Rentsch
16 Nov 24 i +* Re: else ladders practice2Lew Pitcher
17 Nov 24 i i`- Re: else ladders practice1Tim Rentsch
20 Nov 24 i +* Re: else ladders practice3Dan Purgert
30 Nov 24 i i`* Re: else ladders practice2Rosario19
5 Dec 24 i i `- Re: else ladders practice1Dan Purgert
1 Dec 24 i `* Re: else ladders practice4Waldek Hebisch
1 Dec 24 i  `* Re: else ladders practice3Janis Papanagnou
2 Dec 24 i   `* Re: else ladders practice2Waldek Hebisch
2 Dec 24 i    `- Re: else ladders practice1Janis Papanagnou
31 Oct 24 +- Re: else ladders practice1Janis Papanagnou
31 Oct 24 `* Re: else ladders practice217Bart
1 Nov 24  `* Re: else ladders practice216fir
1 Nov 24   +* Re: else ladders practice198Bart
1 Nov 24   i+* Re: else ladders practice196fir
1 Nov 24   ii`* Re: else ladders practice195Bart
1 Nov 24   ii `* Re: else ladders practice194fir
1 Nov 24   ii  `* Re: else ladders practice193fir
1 Nov 24   ii   `* Re: else ladders practice192Bart
1 Nov 24   ii    `* Re: else ladders practice191David Brown
1 Nov 24   ii     `* Re: else ladders practice190Bart
1 Nov 24   ii      `* Re: else ladders practice189David Brown
1 Nov 24   ii       `* Re: else ladders practice188Bart
2 Nov 24   ii        `* Re: else ladders practice187David Brown
2 Nov 24   ii         `* Re: else ladders practice186Bart
3 Nov 24   ii          +- Re: else ladders practice1Tim Rentsch
3 Nov 24   ii          +* Re: else ladders practice4fir
3 Nov 24   ii          i`* Re: else ladders practice3Bart
3 Nov 24   ii          i `* Re: else ladders practice2fir
3 Nov 24   ii          i  `- Re: else ladders practice1fir
3 Nov 24   ii          +* Re: else ladders practice4fir
3 Nov 24   ii          i`* Re: else ladders practice3Bart
3 Nov 24   ii          i `* Re: else ladders practice2fir
3 Nov 24   ii          i  `- Re: else ladders practice1fir
3 Nov 24   ii          +* Re: else ladders practice35David Brown
3 Nov 24   ii          i+- Re: else ladders practice1Kaz Kylheku
3 Nov 24   ii          i+* Re: else ladders practice23Bart
4 Nov 24   ii          ii+* Re: else ladders practice21David Brown
4 Nov 24   ii          iii`* Re: else ladders practice20Bart
4 Nov 24   ii          iii +* Re: else ladders practice2David Brown
5 Nov 24   ii          iii i`- Re: else ladders practice1Bart
5 Nov 24   ii          iii `* Re: else ladders practice17David Brown
5 Nov 24   ii          iii  +* Re: else ladders practice2Bart
5 Nov 24   ii          iii  i`- Re: else ladders practice1David Brown
6 Nov 24   ii          iii  +* Re: else ladders practice5Bart
6 Nov 24   ii          iii  i`* Re: else ladders practice4David Brown
6 Nov 24   ii          iii  i `* Re: else ladders practice3Bart
7 Nov 24   ii          iii  i  `* Re: else ladders practice2David Brown
7 Nov 24   ii          iii  i   `- Re: else ladders practice1Bart
9 Nov 24   ii          iii  `* Re: else ladders practice9Janis Papanagnou
9 Nov 24   ii          iii   `* Re: else ladders practice8David Brown
10 Nov 24   ii          iii    `* Re: else ladders practice7Janis Papanagnou
10 Nov 24   ii          iii     `* Re: else ladders practice6David Brown
19 Nov 24   ii          iii      `* Re: else ladders practice5Janis Papanagnou
19 Nov 24   ii          iii       `* Re: else ladders practice4David Brown
19 Nov 24   ii          iii        `* Re: else ladders practice3Janis Papanagnou
19 Nov 24   ii          iii         `* Re: else ladders practice2David Brown
20 Nov 24   ii          iii          `- Re: else ladders practice1Janis Papanagnou
9 Nov 24   ii          ii`- Re: else ladders practice1Janis Papanagnou
8 Nov 24   ii          i+* Re: else ladders practice9Janis Papanagnou
8 Nov 24   ii          ii+* Re: else ladders practice4David Brown
9 Nov 24   ii          iii`* Re: else ladders practice3Janis Papanagnou
9 Nov 24   ii          iii `* Re: else ladders practice2David Brown
10 Nov 24   ii          iii  `- Re: else ladders practice1Janis Papanagnou
9 Nov 24   ii          ii`* Re: else ladders practice4Bart
9 Nov 24   ii          ii `* Re: else ladders practice3Janis Papanagnou
9 Nov 24   ii          ii  `* Re: else ladders practice2Bart
10 Nov 24   ii          ii   `- Re: else ladders practice1Janis Papanagnou
8 Nov 24   ii          i`- Re: else ladders practice1Bart
5 Nov 24   ii          `* Re: else ladders practice141Waldek Hebisch
5 Nov 24   ii           +- Re: else ladders practice1fir
5 Nov 24   ii           +* Re: else ladders practice24David Brown
5 Nov 24   ii           i+* Re: else ladders practice17Waldek Hebisch
5 Nov 24   ii           ii`* Re: else ladders practice16David Brown
6 Nov 24   ii           i`* Re: else ladders practice6Bart
5 Nov 24   ii           `* Re: else ladders practice115Bart
1 Nov 24   i`- Re: else ladders practice1fir
2 Nov 24   `* Re: else ladders practice17Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal