moonbit-docs
moonbit-docs copied to clipboard
Incomplete sections Conditional Expressions and Loops in README.md
- The code snippets and the text use only expressions in consequent and else clause, but it may be statements.
- What is
to group multiple expressions
in sentenceCurly brackets are used to group multiple expressions in the consequent or the else clause.
?
- yes, contributions to better code examples are welcome.
- With curly braces, one can group multiple expressions into one expression, and the value of the last expression in group will be the value of the entire expression.
One example is:
func init {
let x = {
let y = 1
y + 1
}
print(x) // 2
}
In this case, x
will be 2
@bzy-debug
- For now only the authors and developers of the language can contribute to README because the full description of the syntax/semantic of the language is absent. I can only point to the trivial errors in the text and code snippets in README.
- According to the section Expressions and Statements
let y = 1
is not expression, it is statement.
Also in the Loops section, the compilation of the snippet below fails:
while true { 1 }
P.S. About README: Confucius (孔夫子) said he would "rectify the names" (正名) to make words correspond to reality.
Statement is expression, which's value is of type unit.
It's ok to write the following code:
let x = {let y = 1} // type of x is Unit
The reason why the while code cannot compile is that the block of while should be Unit
type but 1
has type Int
The doc text above uses term "statement":
- Therefore, init functions should only consist of statements.
- MoonBit distinguishes between statements and expressions. In a function body, only the last clause should be an expression, which serves as a return value.