Yield as contextual keyword (was: Call for bikeshed -- break replacement in expression switch)

Guy Steele guy.steele at oracle.com
Mon May 20 16:06:34 UTC 2019


I am becoming more worried again about the consequences of using a contextual keyword such as “yield”, especially for IDEs.

It seems to me that this whole discussion kicked off with a discussion of `yield` as being desirable because, by analogy with `return` being the One True Way to return a value from a stack frame for an invocation, it might be desirable to have One True Way to return a value from a statement-like expression that did not create a stack frame.  So if in future we might have a number of other expressions from which we want to return a value, we might want to pick a keyword not specifically associated with `switch`.

But I now think that this argument is weak, because I believe it is in fact unlikely that there will be a large number of other statement types that we will want to turn into expressions.

	* Yes, we may want block expressions.
	* We would want `if`-statement expressions _except_ that we already have the ternary operator ? :, so I don’t really think there is a pressing need.
	* As a Common Lisp programmer, I can see the value of having a `for` loop produce a value.  But I very much doubt that Joe Java-programmer wants that.
	* And if we don’t want values from `for` loops, I doubt there will be much demand for values from `while` loops or `try` statements.

So I really do think that ? : and  `switch` expressions and blocks really are it.  Sometimes there really *are* only two or three things of real interest, even if one could theoretically dream up more.

And if I am wrong, there is still a way out for the rare cases: if we have a way to do a non-local yield from a block, you can wrap that kind of block around any statement you like, just as you can `break` from any kind of statement by wrapping a labeled-statement around it (that is, by giving it a label).

So I am now leaning toward either `break-with` (or `switch-yield`, but I think `break-with` is better).

And if we ever do expression blocks, then we could have a special thing for returning from them.  In fact, I have a suggestion:

	block expression		   ({  statements; expression  })
	labeled block expression     (label: {  statements; expression  })
	nonlocal block yield            break label: expression;

In fact, if we had those, we wouldn’t need a special way to yield a value from a switch expression; we could just write

	(label: switch { . . . ; case 43: { printf(“foo”); break label: 96; } . . . })

which in turn suggests that we could omit the label from the `switch` statement and the corresponding `break` statement if we wanted—in other words, under this theory a plausible spelling of `break-with` is `break:`.  :-)

—Guy



More information about the amber-spec-observers mailing list