Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern Matching #59

Merged
merged 9 commits into from
May 31, 2021
Merged

Pattern Matching #59

merged 9 commits into from
May 31, 2021

Conversation

mtk3d
Copy link
Contributor

@mtk3d mtk3d commented May 29, 2021

Here is my proposition for pattern matching 馃槂
Unfortunately, the "case", and "match" (the "match" since PHP8.0) are reserved words, so I used the "Generic" prefix, but I'm not sure if this is ok.
I added a new ignore statement to PHPStan. I have no idea where is the problem, everything looks ok, especially that this part was not changed by me 馃 .

@codecov
Copy link

codecov bot commented May 29, 2021

Codecov Report

Merging #59 (f328bed) into master (486f85c) will increase coverage by 0.76%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #59      +/-   ##
============================================
+ Coverage     93.54%   94.30%   +0.76%     
- Complexity      431      463      +32     
============================================
  Files            31       48      +17     
  Lines           899      984      +85     
============================================
+ Hits            841      928      +87     
+ Misses           58       56       -2     
Impacted Files Coverage 螖
src/GenericMatch.php 100.00% <100.00%> (酶)
src/Match/DefaultCase.php 100.00% <100.00%> (酶)
src/Match/DefaultCase/DefaultCaseCallable.php 100.00% <100.00%> (酶)
src/Match/DefaultCase/DefaultCaseStatic.php 100.00% <100.00%> (酶)
src/Match/GenericCase.php 100.00% <100.00%> (酶)
src/Match/GenericCase/GenericCaseCallable.php 100.00% <100.00%> (酶)
src/Match/GenericCase/GenericCaseStatic.php 100.00% <100.00%> (酶)
src/Match/Predicates/IsAllOf.php 100.00% <100.00%> (酶)
src/Match/Predicates/IsAnyOf.php 100.00% <100.00%> (酶)
src/Match/Predicates/IsIn.php 100.00% <100.00%> (酶)
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 486f85c...f328bed. Read the comment docs.

@akondas
Copy link
Member

akondas commented May 30, 2021

Thanks. I appreciate the work.
Nevertheless, since version 8 we have native support: https://www.php.net/manual/en/control-structures.match.php

So I wonder what would that get us? Are there any cases where munus would give something better?

@akondas
Copy link
Member

akondas commented May 30, 2021

What do you think about more functional way (with functions for shorter notations):

$result = matchValue(1)->of(case(isNotNull(), 'match'), case('other'));

That code at the top could actually produce what's at the bottom. Such syntactic sugar 馃槈

$result = GenericMatch::value(1)->of(
    GenericCase::of(Is::notNull(), 'match'),
    DefaultCase::of('other')
);

@mtk3d
Copy link
Contributor Author

mtk3d commented May 30, 2021

Yeah, the functional way looks very nice. How do you think to do that? Just wrap the existing classes into functions?

So I wonder what would that get us? Are there any cases where munus would give something better?

I thought only about the older version of PHP, the native PHP8 match is the best option I think.
This work just gave me some fun and was an exercise for me, so I already get the value for myself. Pull request is the additional thing for me. So if you don't want to add the match for older PHP versions to munus, that's ok, just close this PR 馃槃

@akondas
Copy link
Member

akondas commented May 30, 2021

Will happily merge this pull request, even if native support is better.
And yes, "wrap the existing classes into functions" that was what I meant.

It looks much better than using static functions
munusphp#59 (comment)
@mtk3d
Copy link
Contributor Author

mtk3d commented May 30, 2021

Great! 馃槃
I just added a new commit that wraps matching into functions. It looks much better now 馃挭

@akondas
Copy link
Member

akondas commented May 31, 2021

It turned out really elegant. 馃憦 馃憤
Let this warm on the master for a moment and we will release it 馃殌

@akondas akondas merged commit a59a3e1 into munusphp:master May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants