PatternMatchingInJavaScriptTC39May2018-KatMarchan-npm,Inc.MatchingtheWhat-what?Commonfeatureinmodernlanguages(Rust,Elixir,F#,C#)Destructuring+ConditionalsSortofanadvancedswitch(“​superswitch​”)Common,centrallogicbranchinginlanguagesthatsupportitOkbutwhataboutJS?Okbutwhatelse?Threeseparateproposals:CoreProposal:gh/tc39/proposal-pattern-matchingAs-Patterns:gh/zkat/proposal-as-patternsCollectionLiterals:gh/zkat/proposal-collection-literalsCoreProposal:TheBigPictureNewConditionalStatementSemanticscloselybasedonDestructuringAssignment/BindingMinimalViableProposalkeepsnewfeatureintroductiontoaminimumAvoidsoverlapwithotherexistingproposalsbutkeepscompatibilityinmindAdditionalbehaviorforkedintoCollectionLiteralandAs-PatternproposalsCoreProposal:OverviewCoreProposal:ScopeSemanticsCoreProposal:DesignDecisionsNoFallthrough(neitherexplicitorimplicit)Variablesareirrefutablepatterns:novar-val-basedmatchingObject.is()fornon-collectioncomparisonOnlyonematchparam(nomatch(x,y){...}​)CoreProposal:PerformanceConsiderationsCurrentsemanticsshouldbehighlycompilable(separationbetweenstatic+runtime)PIC-cacheablebranchesbyinternalMap(thev8/Selfthing,nottheuserlandMap)ExistingoptimizationsfordestructuringshouldbereusableCoreProposal:PathologicalCasesShouldtherebeaspecialcaseforsomeofthese?What’sthefullset?Infinityisalreadyspecial-casedinsomesituations,iiuc.CoreProposal:SyntaxQuestionsCurrentlyspeccedtouseNLTH.Thisisnotdesired.Currentalternativesarelacking,butexistingBest+mosteffectivesolutioniss/match/case/gAlsosuperswitch(...){}Drawbacktoexistingreservedwords:superficialconfusionCoreProposal:OtherPendingQuestionsSpecial-caseInfinity/NaN/etc?SomeWIPonspecificsofbehaviorw/Iteratorsand[]-patternsHopingforothertwoproposalsforparticularlyexcitingstuffConsider`​whennewFoo(a,b,c)->​`patterns?CoreProposal:Stage0https://web.archive.org/web/20190716130840/https://github.com/tc39/proposal-pattern-matchingHowaboutStage1?😘SideProposal:As-PatternsExplicitsyntaxforbindingoriginalvalueswhendestructuringBasedon`​import​`syntax’s`​as​`clausesSide-Proposal:As-PatternsUsefulwithbotharraysandobjects,inanyDestructuringBinding:Side-Proposal:As-PatternsBigMotivator:IntegrationwithmatchstatementSide-Proposal:CollectionLiteralsBidirectionalLiteralSyntax(destructuringANDconstruction)EasyconstructionofcollectionsusingfamiliarsyntaxnewMap([[]][][[[][][][][]]][[[][]]][[]]]]]])ishellNicesugaroverprogrammaticprotocolsSide-Proposal:CollectionLiteralsConstruction:Side-Proposal:CollectionLiteralsDestructuring:Side-Proposal:CollectionLiteralsPatternMatching:Side-Proposal:CollectionLiteralsExceptionallyusefulforPatternMatchingHavingall3makesthefeaturemorestraightforwardGreatforintegratingnewstandardandcustomdatastructuresConstructor.from()+iteratorforcreationinstance[Symbol.valueOf]()returnsiteratorformatching