[SUGGESTION] Statement-expressions, result vs return
#643
Replies: 5 comments
|
|
If I understand correctly, his is just |
|
Yes, that's it. Only the syntax is changed to
Also variable capturing, is not needed. |
|
After the decision from @hsutter to shorten the syntax of lambdas from // This part is the same as before.
variable: = {
if condition {
result hello();
}
else {
result bye();
}
}
// This part is changed from `: = {...}` to `{...}`
call({ n: = num(); result n + 0; },
{ n: = num(); result n + 1; });So simply
All of those keywords are within func: () -> int = {
while true {
x: = call({
if condition {
result 1;
}
else {
result 0;
}
});
break;
}
return x;
} |
|
Thanks for your patience. I close this discussion in favor of #836. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Preface
Statement-expression is a solution to have a group of statements in place of an expression.
I suggest to have statement-expressions in Cpp2 with syntax
: = { ... }and the result of them are returned withresultkeyword. In this way, the following code:... is somehow equal to the following code except that we don't have to specify the type of arguments:
Statement-expressions can be used to declare variables too:
Suggestion Detail
Currently we declare functions in Cpp2 in the following syntax:
In a similar syntax, we can declare variables in Cpp2 with statement-expressions:
Statement-expressions are unnamed variables in a similar manner that lambdas are unnamed functions:
resultinstead ofreturnwithin statement-expressions.{ ... }has the lifetime of that scope.{ ... }will be executed one time in their place.{ ... }.Statement-expressions will be not allowed to be used in place of statements. Because it's an error to have an unused value (literal or identifier) in Cpp2:
main: () = { // ERROR! It has to be in place of an expression not an statement. : = { x: = 0; result x + 1; } // ERROR! Because `x + 1` is an unused value (literal or identifier). }Expressions, Functions and Blocks
Now
{ ... }has different meanings in the following categories:: Type = { ... }is a statement-expression in whichTypecan be omitted and deduced from{ ... }.{ ... }ends withresult something;.: (params) -> Type = { ... }is a function or a lambda in which-> Typecan be omitted if the return type isvoid.{ ... }ends withreturn something;or the end of}.x0: (param) = param; x1: (param) = { return param; } call(: (param) = { return param; })(params) { ... }is a parameterized statement block. Whereas{ ... }is a statement block without(params)in which()must be omitted.{ ... }ends with:}.breakandcontinuein loop control structures.result something;when their outer scope is a statement-expression.return something;when their outer scope is a function or a lambda.In all of the above categories, they have this similarity:
{ ... }has the lifetime of that scope.Additinally they have different behaviours in statement execution and variable capturing:
{ ... }one time in their place.{ ... }multiple times in any place.{ ... }.{ ... }.I should mention that statement-expressions, functions and lambdas, statement blocks and parameterized statement blocks can be nested inside each other.
Your Questions
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
Yes. In a way that it helps Xto organize code and separate related statements in nested blocks to easily fix or prevent unwanted bugs which are a result of having mixed unrelated statements together.
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes. In the following ways:
if,whileand other control structures as an expression that is more readable in variable assignment, because the intention is clearly written in code, see the example below.Consider how the following code:
variable: = { if condition { result hello(); } else { result bye(); } }... is more readable and generic than the following code which we have to also specify the type of the variable:
Considered Alternatives
Instead of
resultkeyword, it's possible to use symbols such as=>or nothing (like in Rust). For example:variable: = { if condition { => hello(); } else { => bye(); } }... or this one (like in Rust):
variable: = { if condition { hello() // without ; } else { bye() // without ; } }Howerver I think that having a keyword like
resultor a notation like=>, is more readable than having nothing.References
This suggestion is inspired from discussions in this issue and this paper mentioned by @JohelEGP and this informative comment from @hsutter which describes Unifying Functions and Blocks.
All reactions