Block expressions
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
VB is a statement oriented language but sometimes a computation is difficult to express purely expressions. We've attacked this problem before with the `If` expression but that's always left open the question of whether we should do it for `Select Case` too. [T-SQL has a `CASE` expression](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql) and the topic has been raised again when considering whether an expression form of pattern matching would be needed.
I'm not a big fan of creating a new expression based syntax for everything and I do think preferring statements is more readable. That said, it's worth considering what a potential design would do to the language. Taking a cue from [piecewise function notation](https://en.wikipedia.org/wiki/Piecewise) this proposal is using `{` and `}` to delineate a statement block. Believe it or not but there are surprisingly few ambiguities with these characters. The minimal feature we could consider would be `Select Case`:
```
Dim timeOfWeek = { Select Case Date.Today.DayOfWeek
Case DayOfWeek.Monday To DayOfWeek.Friday
"Work Week"
Case DayOfWeek.Saturday, DayOfWeek.Sunday
"Weekend"
End Select }
```
It seems straight-forward enough what's happening, maybe? It also solves the problem some languages solve with a `Let` statement and would avoid adding a specific `Select Case` or `Match` expression.
Should we have a special keyword for yielding the ultimate value of the expression (e.g. `Yield`). I suspect `Return` wouldn't be ambiguous (since today it can't appear in the middle of an expression) but it could be confusing. What if instead we take a page from the F# manual and say that the value of the block is the value of the last expression statement along an execution path.
That's `Select Case` but if we do that what if we redid `If`? It would solve scenario #97 without changing the behavior of the `If` expression:
`Dim b As Boolean? = { If crmDate.IsNull Then Nothing Else crmDate.Value }`
Inspired by #57 what if we supported more than just `Select Case` and `If`, what if we supported loops?
```
Return
<%= { For Each item in aList
Next } %>
```
That's pretty nice looking, I think. Of course, in this case a loop statement would yield and IEnumerable instead of a single value. Is that too subtle? (**YES!**) What if we required the Yield statement to create an IEnumerable? And maybe we could even just do more statements if we can work out ambiguities.
The reason this could work without syntactic ambiguity is because every executable statement in Visual Basic begins with a keyword with the exception of invocation and assignment which may begin with an identifier, and labels which may begin with an identifier or number so the parser can tell pretty quickly if the curlies contain a statement list or an expression list.
Needs a prototype and we need to see code where this would make the code more readable to determine whether this would make VB more expressive without sacrificing straight-forwardness.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.