DEV Community

Discussion on: How to eliminate if-else chain, for long life software

 
xfbs profile image
Patrick Elsen

Rust has a nice match statement too, even though it is not functional.

match person {
    Person::Employee(employee) if employee.years() > 3 => true,
    Person::Employee(employee) if employee.age() >63 => true,
    Person::Intern(internet) if intern.name().first() == 'S' => true,
    _ => false
}
Enter fullscreen mode Exit fullscreen mode

I believe it is something that most functional language have and newer languages are starting to adopt it.

Thread Thread
 
delta456 profile image
Swastik Baranwal

Almost every modern language have pattern matching.

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Tree patterns are used in some programming languages as a general tool to process data based on its structure, e.g. C#,[1] F#,[2] Haskell,[3] ML, Python,[4] Ruby,[5] Rust,[6] Scala,[7] Swift[8] and the symbolic mathematics language Mathematica have special syntax for expressing tree patterns and a language construct for conditional execution and value retrieval based on it.

In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: "either it will or will not be a match." The patterns generally have the form of either sequences or tree structures. Uses of pattern matching include outputting the locations of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence.

Thread Thread
 
dr_nicole profile image
Dr Nicole Jacqueline Martin

The techniques above are called pattern matching. I wrote an article about it here:
dev.to/n1ckdm/pattern-matching-dec...

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Really good article thanks
Not that Kotlin when is not true pattern matching found in more fiftieth languages, it's a better switch case. Combined with static types and algebraic types it's 80% of that though

Thread Thread
 
matthewbdaly profile image
Matthew Daly

Newer versions of PHP have match.