Deprecate ReturnStatements?

Manfred_Nowak svv1999 at hotmail.com
Sun Jan 2 05:01:47 PST 2011


Walter Bright wrote:
> writing generic code so that the same code can be generated for void
> and non-void return values.
  http://d.puremagic.com/issues/show_bug.cgi?id=5399 (cited 01/02/11)

The docs include:
| Expressions that have no effect, like (x + x), are illegal in
| expression statements. If such an expression is needed, casting it to
| void will make it legal.
  http://www.digitalmars.com/d/2.0/statement.html#ExpressionStatement
| If the Expression has no side effects, and the return type is void,
| then it is illegal.
  http://www.digitalmars.com/d/2.0/statement.html#ReturnStatement
  ( both cited 01/02/11)

Walters remark suggests that the dysmorphism between returnStatement and 
expressionStatement is based on arbitrariness: generating an expression 
by generic code must still take into account, whether the genrated 
expression will be used for an expressionStatement or a returnStatement.

This is because casting to void will not legalize an expression without 
side effects for a returnStatement, but for an expressionStatement only.

To make this homomorphic it might be adequate to view returning as an 
attribute of an expressionStatement:

  `(void).return;' instead of `return;'   whithin `void f(){}'
  `(1).return;'    instead of `return 1;' whithin `int f(){}'
 
and `(cast(void) 1).return;' whitin `void f(){}' to make returning a 
constant to void as legal as using a constant as an expression:
  `cast(void) 1;'.

-manfred


More information about the Digitalmars-d mailing list