0

C#10 Has extended the property pattern ability, I think it began since #7

EG:

{ Car: { Interior: pattern } }

Meaning you can do some nice pattern matching even in if statements

if (car_var is Interior leather) { .. }

It's been extended to manage switches etc.

In TS: using filters (maybe not), I would like to write a helper method that could perform this.

if(car is Leather { size: large, colour: black}) {
 // do specific work for this model definition
 }

I could define a model library that would be able to behave like constants in TS.

I can see there is a proposal, here but I haven't found a clean // nice way to do it, and I'm not sure if I write a library that overwrites the if check is the right way.

If I write my own, how can I ensure it's forward compatible and what are things to look out for or be aware of ?

Note: I do contribute to the Opensource of TypeScript but not as much as this would involve.

1 Answer 1

2

There are two ways to do this in a standard and future proof way.

  1. Update the language by contributing type matching to type script in this way, and then using that syntax in your own programs.

  2. Write a library using functions, and over-loadable operators to implement the desired behaviors. Then write your software using that library.

Precluding a breaking change to syntax (such as changing the pattern matching syntax, dropping support for overriding those operators, or how functions are implemented called) then both of these approaches result in future proof code.

There are a few other ways to solve this problem:

  • Don't use type script find some other language to do the job
  • Don't use pattern matching spell it out instead
  • Invent your own trans-piled language

Not the answer you're looking for? Browse other questions tagged or ask your own question.