-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Proposal) Match Expression #191
Comments
An attempt at parsing the simple grammar in #216 /* Digit ::= '0' - '9' */
Func<char,bool> Digit = (c)=>{
match ( c ) with
{
| '0'-'9' => true.
| _ => false;
}
};
/* Separator ::= '_' */
Func<char,bool> Separator = c =>{
match ( c ) with
{ | '_' => true;
| _ => false;
}
}
/* Literal ::= Digit ( Separator? Digit)* */
match (curr) with
{
| _ when Digit => { var peek = curr;
do
{
peek = peek.Next();
} until ( match (peek) with
{
| _ when Separator => { var peek2 = peek.Next();
match (peek2) with
{ | _ when Digit => {
peek = peek2;
return true;
}
| _ => false;
}
}
| _ when Digit => true;
| _ => false;
}
)
}
| _ =>;
} I don't it look that bad. |
Usng the match slightly differently. match ( curr )
{ | _ when Digit => var peek1 = curr;
var peek2 = curr;
do
{
peek1 = peek1.Next()
peek2 = peek1.Next()
match ( peek1 , peek2 )
{ | (_,_) when ( Separator , Digit ) => peek1 = peek2;
| (_,_) when ( Digit , _ ) => peek1 = peek1;
| _ => exit do;
}
};
| _ => ;
} By some for a prefix it could be little smaller. match ?? ( curr )
{ | Digit => var peek1 = curr;
var peek2 = curr;
do
{
peek1 = peek1.Next()
peek2 = peek1.Next()
match ?? ( peek1 , peek2 )
{ | ( Separator , Digit ) => peek1 = peek2;
| ( Digit , _ ) => peek1 = peek1;
| ( _ , _ ) => exit do;
}
};
| _ => ;
} VB.net Example Dim Digit As Func(Of Char,Boolean) = Function( c )
Match ( c )
Case "0"c To "9"c : Return True
Case Else
Return False
End Function
Dim Separator As Func(Of Char,Boolean) = Function( c )
Match ( c )
Case "_" : Return True
Case Else
Return False
End Math
End Function
Match (curr)
Case _ When Digit
Dim peek = curr
Do
peek = peek.Next()
Loop Until Match (peek )
Case _ When Separator
Dim peek2 = peek.Next()
Match (peek2)
Case _ When Digit : peek = peek2 : Return True
Case Else
Return False
End Match
Case _ When Digit : Return True
Case Else
Return False
End Match
Case Else
End Match
Match ( curr )
Case _ When Digit
Dim peek1, peek2 = curr
Do
peek1 = peek1.Next
peek2 = peek1.Next
Match ( peek1 , peek2 )
Case ( _ , _ ) When ( Separator , Digit ) : peek1 = peek2
Case ( _ , _ ) WHen ( Digit , _ ) : peek1 = peek1
Case Else
Exit Do
End Match
Loop Until False
Case Else
End Match |
I like your idea but is something like Nemerle does and to be honest, I can lost myself easily on something like this: match ( e , a ) with
{
| ( null, null ) => ... ;
| ( _ , _ ) into
{l:= a.Left as IdentifierName;
r:= a.Right as IdentifierName;
} => match (l,r) with
{
| (null,null) => ... ;
| ( _ , _ ) when (a.Name.name == r.Name.name) => ... ;
| _ => ... ;
};
| _ => ... ;
} But I agree with you, it's powerfull! |
What's the point if the |
@GeirGrusom The syntax is from Nemerle.
|
Sorry I meant terminal. I think in order to look more like C# it should use |
Match Expression
This proposal is separated out from #180. The approach to the syntax is an alternative to the one proposed by @MadsTorgersen
A match expression block is an expression as it permits uses in situations that switch can not be used. For example in-conjunction with a return
or an 'await' this needs further investigation
|
donates the start of a match clause
into
allow you to introduce new variables into the scope (of this match clause), or reuse existing in scope variables.
**
:=
is for introducing a new variable into scope.when
is for specifying that this match clause is considered if the predicate is satisfied.
=>
this doesn't indicate the the expression after is a lambda function, it just reusing an existing symbol as it's is close to what you use for lambda. Eg multiple statements / expression are required to be enclosed inside braces{ ... }
. Not needing areturn
for simple single expressions.The code called when the match clause is satisfied could be an expression, a statement or an exception.You can't intermingle expression and statements.
Eg
The rationale for
into
beforewhen
is that is allows the variable just introduced to be used with the predicate of the of the guard expression (when)....
is being use in the following examples as a place-holder for some code.Default Clause
Every match is required to have default clause, which is used if non of the other clause are satisfied. Examples will use
| _ =>
to represent this. If all of args in the "target" are wildcards this is also consider a default clause. A default clause most also have no guards, so it can catch all possibilities..Examples
Grammar
How the your code looks when using the form being proposed is clean looking and fit in well is existing C# style. It's not too far from existing style to be jarring, when creating the examples it felt (to me) very natural.
The text was updated successfully, but these errors were encountered: