You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Break down functionality into smaller pieces, make things more teachable.
Matching becomes a single primitive that can be brought into several constructs, including maybe a new match expression.
let when, etc.
let { body } when isOk(response);
Some concerns about this
No initializer
Single unary function is very specialized.
Doesn't work for arrays due to iterable (but this is solvable).
"Failure mode" for a match is silent - just falls back to {}.
Integration with control flow expressions
if (let { body } when isOk(response)) {
// ...
}
for (let { body } when isOk of responses) {
// ...
}
while (let { body } when isOk(responses.pop())) {
// ...
}
Some of these examples echo something very intriguing - but it seems like there's room to integrate on the how of making these consistent and compositional.
Then match expressions would invert the difference between matching and binding.
Bindings now would need an explicit keyword instead of becoming the default behavior.
Matching now becomes the default behavior.
We highly prefer explicit binding - though we don't necessarily feel it should be separated from the pattern.
Extractor Objects Addition
Inspired by Scala's apply/unapply if you're familiar, but not crucial.
Allows you to create a local from expression position.
if (container is Box and { containedValue: let value }) {
// 'value' is now scoped and was set to 'container.value'.
}
Like the proposal direction.
Cute that you can introduce new bindings - would prefer a let ... in sort of syntax.
Not certain about matching on type.
Wait, what does ${ and } from the original proposal mean again?
It's the difference between matcher vs binding. ${Foo} would say "the thing inside of the curlies (Foo) is a matcher" because otherwise you'd create a variable named Foo
"What?"
Seems like this prioritization is backwards - matching an existing binding is the common case.
The text was updated successfully, but these errors were encountered:
I have an idea from the #15480 relate to pattern matching that, in typescript we could extend this feature more than C# to allow pattern matching directly as type constraint in addition to if and switch. That would allow solving #15480 and greatly satisfied similar case of type constraint for every type in almost any use cases
For example
functionGrade(grading: numberwhen(>=0)&(<=100)&(%10==0)){// grading are constraint to be 0 10 20 30 40 ... 90 100}Grade(10);// fineGrade(19);// errorGrade(120);// errorletvalue=GetNumberValue();// numberif(value>=0&&value<=100&&value%10==0)Grade(value);// should not error if possible
Pattern Matching
[[Overview of current pattern matching proposal.]]
Looks roughlylike
All RHSs are expressions, not statements. If you need statements, you need a
do
expression which doesn't exist yet.What patterns are there?
and
Patternor
Pattern${
Expression}
Infinity
NaN
undefined
0
,"some string"
,null
,RegularExpressionLiteral
, andTemplateLiteral
.match ([1]) { when ([1, undefined]) ...
- does this match?const [x, y] = [1]
One can use a value with a custom matcher within an
${
interpolation pattern}
.Symbol.matcher
to return the things that you want to allow a user to bind/destructure after the pattern match.with
keyword that allows you to bind against that value.with
destructuring.Control-flow analysis implications for TypeScript
A match should narrow based on the implications of the thing being matched against.
One counter-proposal around "epics"
Break down functionality into smaller pieces, make things more teachable.
Matching becomes a single primitive that can be brought into several constructs, including maybe a new
match
expression.let when
, etc.{}
.Integration with control flow expressions
Then
match
expressions would invert the difference between matching and binding.Extractor Objects Addition
apply
/unapply
if you're familiar, but not crucial.Epics + Extractor Objects
is
expressionis
MatchPatternis
integration with patterns if you're familiar with it, but not important.Allows you to match and bind.
Allows you to create a local from expression position.
Like the proposal direction.
let ... in
sort of syntax.Wait, what does
${
and}
from the original proposal mean again?${Foo}
would say "the thing inside of the curlies (Foo
) is a matcher" because otherwise you'd create a variable namedFoo
The text was updated successfully, but these errors were encountered: