TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Pattern Matching Support

Open dignifiedquire opened this issue 12 years ago • 64 comments

I've seen some short notions about pattern matching on code plex, but no serious discussions around it. As I'm a big fan of it, I've written up some small examples on how I think pattern matching could look in TypeScript.

This is by far no complete specification, just some ideas that I want to bring forward and have a little discussion around.

Function Overloading

// Pattern matching on functions

function pickCard {
  (x: {suit: string; card: number; }[]): number {

    var pickedCard = Math.floor(Math.random() * x.length);
    return pickedCard;  

  }
  (x: number): {suit: string; card: number; } {

    var pickedSuit = Math.floor(x / 13);
    return { suit: suits[pickedSuit], card: x % 13 };

  }
} 
// Translation of functions.ts using sparkler
// https://github.com/natefaubion/sparkler

function pickCard(x) {
  [...{ suit @ String, card @ number }] =>  {
    var pickedCard = Math.floor(Math.random() * x.length);
    return pickedCard;  
  }
  x @ Number => {
    var pickedSuit = Math.floor(x / 13);
    return { suit: suits[pickedSuit], card: x % 13 };
  }
} 

Match statement

// Pattern matching using match

function whoami (x: any): string {

  match x {
    case { name: string, type: 'person' }: 
        return 'Person: ' + name;

    case { name: string, type: 'animal' }: 
        return 'Animal: ' + name;

    default: 
        return 'Unkown you';
  }
} 
// Pattern matching using match translated

function whoami (x) {

  match x {
    case { name @ String, type: 'person' }: 
        return 'Person: ' + name;

    case { name @ String, type: 'animal' }: 
        return 'Animal: ' + name;

    default: 
        return 'Unkown you';
  }

} 

dignifiedquire avatar Jul 21 '14 19:07 dignifiedquire

What about this ECMAScript proposal? There's a couple implementations of it and the syntax looks like it would fit well. I think this shows that it's at least possible. https://github.com/tc39/proposal-pattern-matching

just-chillin avatar May 01 '20 08:05 just-chillin

Very likely there's no movement on this issue until that proposal reaches stage 3.

Edit: I don't much like it either folks, but check TypeScript's project goals and its past behaviour, there's zero chance of a massive break from the standards on this, and implementing a standard too early has already backfired once.

samhh avatar May 01 '20 09:05 samhh

Is there any proposal\issue on ternary operator, like

return anyArray.find({condition}) let item ? computation_over(item.{prop}) : undefined?

Lonli-Lokli avatar Jul 21 '20 10:07 Lonli-Lokli

Pursuant to our goals of a) not adding type-directed emit and b) not getting ahead of TC39 on runtime features like this, I'm going to close this as out of scope for the time being. When https://github.com/tc39/proposal-pattern-matching gets to Stage 3 we'll implement this ASAP, but no sooner. Further discussion on how that feature should work belongs entirely in the proposal repo, so I'll be locking this to better channel discussion to the correct place. Thanks!

RyanCavanaugh avatar Jul 21 '20 17:07 RyanCavanaugh