Branching or Exit Points : Quad Saga


#1

Branching or Exit Points : Quad Saga

  • Consistency : promotes simplicity
  • Simplicity : promotes easy learning.

Names for the 4 functions :

  • fn branchOut() returns ()
  • fn branchTo(Label) returns ()
  • fn branchWith() returns Val
  • fn branchIf(BoolExp) returns ()

Syntaxes of the 4 Expressions :

  • break; or break ();
  • break 'label;
  • break val;
  • break if x > y; ?

we need ‘break if’ expression.

‘if’ is the filter for the exit point : exit iff true.

Usage : inside ‘loop’ block

loop {

break if x > y;

// equivalent to js : while (x > y) {… }

… }

loop { …

// equivalent to js : do{ … } while (x > y);

break if x > y; }

loop { …

// see ada-lang : ‘exit when’ syntax for an equivalent. // 2 or more can be placed anywhere in the the loop, scattered or not.

break if x > y;

… }

Note : that is a lot of flexibility and expressive powess. Yet the rust core team keeps it from us ?? :slight_smile:

Please correct the wrong ?? :slight_smile:

when teaching the branching concept to school students, leaving one component out is a disservice. they need complete understanding and practice of the concept.

Please Help Us Out. THANKS.


#2

What are some reasons to prefer break if x > y over if x > y { break }?


#3

@dtolnay the ‘if’ keyword is being used as a filter.

we can get rid of if statement or expression, if need be

we can get rid of while(){} statement or expression also

I am trying to promote consistency, which may lower the learning

curve barrier: ‘On Boarding’.

And use match or loop expression or statement instead.

less syntax -> less to learn


#4

the ‘if’ keyword is being used as a filter.

that is a very arbitrary distinction to make, there’s no real difference between the two constructs - you seem to be suggesting that the design of the AST is important for a beginner to the language. Are you sure you’re not just arguing for this syntax because it reads more like natural language?

less syntax -> less to learn

So how does adding more redundant syntax help?


#5

To clarify, in your code is the behavior identical to if x > y { break }?


#6

@dtolnay and @Diggsey

I am discussing the break exit points as a set of four entities.

they are all related to the same concept : exit points.

that is the consistency.

‘break if’ is the last one remaining.

Yes its semaintics is equivalent to : if (x>y) {break;}

but that is not an exit-point or branching.

  • break;
  • break 5;
  • break 'label;
  • break if x > y;

CONSISTENCY :slight_smile:

I personally do not use the while keyword.

it is conditional repetition. and not that flexible at all.

‘loop’ keyword + ‘break if’ combine give great flexibility.

see the many ways of using them in the original post.

we can deprecate ‘while’ and ‘if’ statement altogether.

one syntax in : two syntax out


#7

we can deprecate ‘while’ and ‘if’ statement altogether.

While we’re at it, we can deprecate for loops and block expressions too. And we can also get rid of bare break; best be explicit with break if true:

// old rust
let sorted = {
    let mut vec: Vec<_> = iter.collect();
    vec.sort();
    vec
};

// new, idiomatic rust
let sorted = loop {
    let mut vec: Vec<_> = iter.collect();
    vec.sort();
    break vec if true;
};