Block Expressions with Consistent Syntax to Declarations #836
Replies: 5 comments 12 replies
-
Another example: var1: i32 = {
print("Hello!");
print("Say something:");
while true {
var2: string = get_user_input();
if var2.to_lowercase() == "exit" {
print("Goodbye.");
= "";
break;
}
else if var2 != "" {
print("Thanks.");
= var2;
break;
}
print("Try again!");
}
}; If a statement starts with |
Beta Was this translation helpful? Give feedback.
-
I think changing declarations is a big change, but I do want do expressions in cpp2. Thet can just translate to IILEs on the cpp side. Other than that, if expressions are also great (idk about the |
Beta Was this translation helpful? Give feedback.
-
I've to explain the reason of why Currently Cpp2 has var1: i32 = inspect x < 10 -> bool { is true = x; is _ = x - 10; }; The assignment operation is right to left (as it's the behavior in Cpp1 too):
Considering var1: i32 = { if x < 10 = z = y = x; else = 0; }; In a similar way:
In a nutshell, I mean if we read the assignment operation from right to left (as it's the behavior in Cpp1 too), it'll be a natural syntax in Cpp2. EDIT: Let's assume |
Beta Was this translation helpful? Give feedback.
-
Should we support multiple statements in each branch? If so we could use a parameterized var1: i32 = (out r) if x < 10 {
foo(x);
r = x;
} else r = x - 10; Note |
Beta Was this translation helpful? Give feedback.
-
Block expressions make it possible to have loop with a half or loop with test in the middle (AKA Dahl loop): (copy name: string = "", copy x: i32 = 0)
while { name = get_name();
= x < 10 && name != ""; } next x++
{
print("(name)$: (x)$\n");
} Without block expressions, it would be like this using infinite loops: (copy name: string = "", copy x: i32 = 0)
while true next x++
{
name = get_name();
if ! (x < 10 && name != "")
{
break;
}
print("(name)$: (x)$\n");
} I'm not saying we need this feature in |
Beta Was this translation helpful? Give feedback.
-
Preface
I suggest to make Cpp2 to be Expression Oriented Programming Language as much as it doesn't harm similarity with C++. In a way that every control structure (such as
if
andfor
) can be used inside expressions.My suggestion is based on the new declaration syntax as suggested in issue #824 and discussion #757.
Briefly the new syntax changes
= {...}
to{...}
:Also the terser function syntax is changed from
: (x) x
to: (x) = x
.This new syntax for declarations is required. It leads to have definitions uniformly with either statement notation
{...}
or expression notation= ...;
. Control structures (such asif
andfor
) can be syntactically and semantically similar to declarations in the same way.First, please read discussion #643 and paper P2806R1 about
do
Expressions. Let's name it Block Expressions. The syntax of a block expression is like this:Instead of
return 0;
, we write= 0;
. It means the value of{}
is equal to0
inside the expression. In this way,= ...;
will be a uniform syntax in all declarations and control structures. Also;
is required after{}
, because{}
is an expression (it's not a block statement), and the whole code is a single statement.Description
Considering
if
control flow:With the help of block expressions, we can write the above example like this:
As you see, the whole
if
control flow is put inside a block expression with{}
. It ends with;
, so you can assume we replaced0
with{ if ... }
, and we replaced allvar1 =
s with=
s. That's it.{}
is optional inif
when it contains= ...;
in block expressions, so let's drop them:Let's compact it:
And compact it again to a single line:
Finally, we have used
if
control flow inside an expression to set the value ofvar1
. It's syntactically similar toinspect
expression in Cpp2:The expression value is defined with
= ...;
in bothif
andinspect
expression. Also{}
in both of them is not a block statement.In the same way we can use
for
andwhile
in block expressions:Consistency
Now I write function declarations and control structors when they are used in block expressions to see how much they are consistent.
A function declaration:
fnc1: (x) = x + 1;
A
for
loop inside block expression:They are consistent with
= ...;
. They both end with;
.A function declaration:
fnc1: (x) { print(x); }
A
for
loop:They are consistent with
{}
. They don't end with;
.The same consistency can be checked for all declarations and control flows. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions