moonbit-docs icon indicating copy to clipboard operation
moonbit-docs copied to clipboard

Incomplete sections Conditional Expressions and Loops in README.md

Open duselguy opened this issue 1 year ago • 4 comments

  1. The code snippets and the text use only expressions in consequent and else clause, but it may be statements.
  2. What is to group multiple expressions in sentence Curly brackets are used to group multiple expressions in the consequent or the else clause.?

duselguy avatar Sep 21 '23 14:09 duselguy

  1. yes, contributions to better code examples are welcome.
  2. 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 avatar Sep 22 '23 01:09 bzy-debug

@bzy-debug

  1. 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.
  2. 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.

duselguy avatar Sep 22 '23 12:09 duselguy

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

bzy-debug avatar Sep 27 '23 11:09 bzy-debug

The doc text above uses term "statement":

  1. Therefore, init functions should only consist of statements.
  2. MoonBit distinguishes between statements and expressions. In a function body, only the last clause should be an expression, which serves as a return value.

duselguy avatar Sep 27 '23 20:09 duselguy