Skip to end of metadata
Go to start of metadata

Statements and blocks can be defined to have values. Thus, statements and expressions are unified.

Advantages:

  • more expressiveness (e.g., more freedom to define temporaries)
  • more consistent with block syntax (which puts statements inside exprs)
  • avoids duplicating useful functionality (as in if/else vs. ?:)
  • it works well in Lisp, Ruby, etc.
  • avoids the need for a special syntax for value-return block closures

Disadvantages:

  • might be misused (but there's no substitute for good taste)
  • might lead to difficult-to-read code (a good convention would help)

Proposed rules:

An non-closed block can be used wherever an expression is allowed, and directly produces the required expression value. A closed block immediately produces a closure. If and whenever the closure is invoked, it produces a return value which is the same as a corresponding non-closed block would have produced directly.

(The context-dependent rules which define when a block is closed or non-closed are defined as part of the closure syntax.)

The value of a block is defined to be the value produced by its last-executed sub-form (expression, declaration, statement, etc.).

If a labeled block is exited normally by a 'break' (or 'return' or 'continue') statement, the value produced by the block is defined to be that of the expression mentioned in the 'break' (if any) else void. The value of an empty block is void.

Examples:

See also http://wiki.codehaus.org/groovy/UnifyExpressionsAndStatements.

  • No labels