Better let, do, next, and loop syntax #1055
Replies: 3 comments 11 replies
-
This is it of changes, and a departure from any syntax I'm used to in various languages, could you perhaps explain the advantages of each? And the pros/cons for the syntaxes each change would replace?
On 20 April 2024 23:38:38 Derp McDerp ***@***.***> wrote:
forever loops
cpp2 should use loop as a synonym for while (true)
loop {
body;
}
while
Then the while keyword should have 2 forms:
while test { body; }
while test; (which can appear anywhere inside of a loop and is a synonym for if (!bool(test)) break;)
loop {
while test;
body;
}
while test {
body;
}
do while
do { body; } while (test); is replaced with:
loop {
body;
while test;
}
for
We can now do a similar thing to for loops:
for range do (i) body;
is replaced with:
for i := range { body; }
for i := range; (which can only appear at the top of a loop body), i.e.
for i := range(0, 30) {
body;
}
loop {
for i := range(0, 30);
body;
}
// parallel iteration, whichever range ends first terminates the loop
loop {
for i := range(0, rand());
for j := range(0, rand());
body;
}
next
The next statement can be attatched to blocks inside of a loop:
for i := range(0, rand()) {
{
next cout << " world";
next cout << "Hello";
if coin_flip() {
continue;
}
next cout << "this is dead code";
}
{
next cout << "whatever";
if coin_flip() {
continue;
}
}
body;
}
=>
for i := range(0, rand()) {
if (coin_flip()) {
cout << "Hello";
cout << " world";
continue;
}
if (coin_flip()) {
cout << "whatever";
continue;
}
body;
}
init
The init statement is like the static keyword from C++ but for first time the loop is entered. These can only appear at the top of the loop:
loop {
init i := 0;
init cout << "first time";
while i < 20;
next ++i;
body;
}
=>
for (auto i = 0, _ = void(cout << "first time"); i < 20; ++i) {
body;
}
loop expressions
loop (init) { body; }
loop (somevar: type = init) { body; }
loop can take an init. If it has that then it is an expression which evalutes to init or somevar:
str: string = loop (s: string = "") {
for _ := range(0, rand());
s += "*";
}
=>
str: string;
{
s: string = "";
loop {
for _ := range(0, rand());
s += "*";
}
str = s;
}
…________________________________
let / do / result
With these changes you now can free up the do keyword for better uses.
In current cpp2 you can do:
(x: i32) body;
(x: i32) { body; }
I'd suggest removing that bizarre syntax and replacing it with do.
do (i: i32 = init) expr_body
do { statement_body; } (this can be a statement or expression depending on if statement_body contains at least one result keyword)
do (i: i32 = init) statement_body (this can be a statement or expression depending on if statement_body contains at least one result keyword)
if the statement_body form of do contains at least one result keyword then the do is an expression which evaluates to the result. Otherwise it is a statement.
x: i32 = do { result foo(); }; // expr
=>
x: i32 = foo();
do { foo(); } // statement
=>
{ foo(); }
asdf: int = do if coin_flip() { result foo(); } else { result bar(); };
// =>
asdf: int;
if coin_flip() { asdf = foo(); } else { asdf = bar(); };
do0
do0 (init) expr
do0 (init) statement
do0 (somevar: type = init) expr
do0 (somevar: type = init) statement
is an expression which evaluates to the init or somevar after executing the body. It's like prog0 from Common Lisp.
—
Reply to this email directly, view it on GitHub<#1055>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQJT7MEQX3U67HQSGNTY6LU6XAVCNFSM6AAAAABGQ2QLVGVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZWGUZTGOJQGM>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
how about this? |
Beta Was this translation helpful? Give feedback.
-
These are interesting ideas, but I don't plan to change the loop syntax. The main question to ask is, Is it a syntactic preference thing, or is it solving a real known problem (safety or complexity) in C++? In an early version of Cpp2, I actually did decompose loops even more generally into a single However, even in a green-field design, I don't think I would put "next" or similar in the body. There's enormous value in declaratively stating the loop basics up front. |
Beta Was this translation helpful? Give feedback.
-
forever loops
cpp2 should use
loop
as a synonym forwhile (true)
while
Then the while keyword should have 2 forms:
while test { body; }
while test;
(which can appear anywhere inside of aloop
and is a synonym forif (!bool(test)) break;
)do while
do { body; } while (test);
is replaced with:for
We can now do a similar thing to
for
loops:for range do (i) body;
is replaced with:
for i := range { body; }
for i := range;
(which can only appear at the top of aloop
body), i.e.next
The
next
statement can be attatched to blocks inside of aloop
:=>
init
The
init
statement is like thestatic
keyword from C++ but for first time the loop is entered. These can only appear at the top of theloop
:=>
loop expressions
loop (init) { body; }
loop (somevar: type = init) { body; }
loop
can take an init. If it has that then it is an expression which evalutes toinit
orsomevar
:=>
let / do / result
With these changes you now can free up the
do
keyword for better uses.In current cpp2 you can do:
(x: i32) body;
(x: i32) { body; }
I'd suggest removing that bizarre syntax and replacing it with
do
.do (i: i32 = init) expr_body
do { statement_body; }
(this can be a statement or expression depending on ifstatement_body
contains at least oneresult
keyword)do (i: i32 = init) statement_body
(this can be a statement or expression depending on ifstatement_body
contains at least oneresult
keyword)if the
statement_body
form ofdo
contains at least oneresult
keyword then thedo
is an expression which evaluates to the result. Otherwise it is a statement.x: i32 = do { result foo(); }; // expr
=>
x: i32 = foo();
do { foo(); } // statement
=>
{ foo(); }
do0
do0 (init) expr
do0 (init) statement
do0 (somevar: type = init) expr
do0 (somevar: type = init) statement
is an expression which evaluates to the
init
orsomevar
after executing the body. It's likeprog0
from Common Lisp.Beta Was this translation helpful? Give feedback.
All reactions