Extended "When" Expressions usage.
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
`When expr` has pre-existing use case.
* Filtered Exceptions
I think a lot of these issues could be solved if make `When` a binary operator, especially in combination with `Select Case`, even more so with `When` guard clauses.
**Grammar**(Roughly)
```
WhenExpr ::= expr "When" expr WhenElse?
WhenElse ::= "Else" expr
```
```
Operator [When]( L As DeclarationExpression , R As Expression(Of Boolean) ) As Expression
Operator [When]( L As Expression , R As Expression(Of Boolean) ) As Expression
```
**Note:** `Out` in these examples is defined as having the following meaning
> If the referred to variable is already declared and in scope use that instance, provided it is a compatible type else it is an error. If referred to variable isn't declared or in scope, then declare and instance of the variable with the same type as the parameter is referencing.
------
**Initialisation / Declaration**
```vbnet
Dim x When Integer.TryParse( Text, Out x )
Dim x When Integer.TryParse( Text, Out x ) Else GoTo error_state
Dim x When Integer.TryParse( Text, Out x ) Else Throw UnspeakableException()
```
*I'm not too sure about that one, the `If` form seems a better fit*
----------
**While Loops**
```vbnet
While ( Dim bytesRead = stream.Read(buffer, 0, buffer.Length) When (bytesRead > 0 ) )
' Use the data you've read
End While
```
*That form I kinda like*
-------
**Select Case**
```vbnet
Function ParseTypeCharacter( here As Integer ) As SyntaxNode
Select Case ch When TryGet(here, Out ch)
Case "U"c, "u"c
Select Case nc When TryGet(here+1, Out nc)
Case "I"c, "i"c : Make_Unsigned_Integer(here)
Case "L"c, "l"c : Make_Unsigned_Long(here)
Case "S"c, "s"c : Make_Unsigned_Short(here)
Case Else : Unexpected_Value(nc)
Else Select
Return Unexpected_EOS
End Select
Case "I"c, "i"c : Make_Signed_Integer(here)
Case "L"c, "l"c : Make_Signed_Long(here)
Case "S"c, "s"c : Make_Signed_Short(here)
Case Else : Unexpected_Value(ch)
Else Select
Return Unexpected_EOS
End Select
End Function
```
or
```vbnet
Function ParseTypeCharacter( here As Integer ) As SyntaxNode
Select Case ch When TryGet(here, Out ch) Else Unexpected_EOS
Case "U"c, "u"c
Select Case nc When TryGet(here+1, Out nc) Else Unexpected_EOS
Case "I"c, "i"c : Make_Unsigned_Integer(here)
Case "L"c, "l"c : Make_Unsigned_Long(here)
Case "S"c, "s"c : Make_Unsigned_Short(here)
Case Else : Unexpected_Value(nc)
End Select
Case "I"c, "i"c : Make_Signed_Integer(here)
Case "L"c, "l"c : Make_Signed_Long(here)
Case "S"c, "s"c : Make_Signed_Short(here)
Case Else : Unexpected_Value(ch)
End Select
End Function
```
*This form I like a lot*
-----------------------------
**Grammar**(Roughly)
```
When_Expr ::= "When" expr
SelectCase_Kind ::= "Case" | "Type"
SelectCase_Header ::= "Select" SelectCase_Kind? expr When_Expr?
SelectCase_Else ::= "Else" SelectCase_Header
SelectCase_Footer ::= "End" "Select"
SlectCase_Block ::= SelectCase_Header SelectCase_Clause* SelectCase_Else* SelectCase_Footer
SelectCase_Clause ::= ( Case_Clause | Case_Clause_Else ) When_Expr?
Clause_Else ::= "Case" "Else"
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.