feat: add pred(callable) — first-class predicate pattern#15
Conversation
Agent-Logs-Url: https://github.com/sentomk/patternia/sessions/0dce48e7-ecdc-43e2-a5f7-a61a05983338 Co-authored-by: sentomk <115908952+sentomk@users.noreply.github.com>
pred(callable) — first-class predicate pattern
There was a problem hiding this comment.
Pull request overview
Adds a new first-class pred(callable) pattern to Patternia, enabling match-time evaluation of unary predicates as non-binding patterns (usable in on(...) and composable with any/all), along with build/test/docs wiring.
Changes:
- Introduces
include/ptn/pattern/pred.hppimplementingpred_pattern<Callable>and thepred()factory (non-binding). - Exports
ptn::predvia the umbrella header and registers the header in CMake. - Adds a dedicated GTest TU and wires it into the tests build; updates the roadmap status.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
include/ptn/pattern/pred.hpp |
New predicate pattern implementation + binding contract specialization. |
include/ptn/patternia.hpp |
Includes pred.hpp and exports ptn::pred in the public API. |
CMakeLists.txt |
Adds the new public header to PTN_HEADERS. |
tests/tests_pred_pattern.cpp |
New unit tests covering basic matching and composition scenarios. |
tests/CMakeLists.txt |
Adds the new test translation unit to ptn_tests. |
docs/roadmap.md |
Marks pred(callable) as finished and references a new release entry. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| constexpr bool match(Subject &&subj) const noexcept(noexcept( | ||
| static_cast<bool>(fn(std::forward<Subject>(subj))))) { | ||
| return static_cast<bool>(fn(std::forward<Subject>(subj))); | ||
| } |
There was a problem hiding this comment.
pred_pattern::match calls the stored callable via fn(...), which prevents using certain valid “callable” forms such as pointers-to-member functions/data members. If the intent is to lift any unary predicate, consider using std::invoke(fn, subj) (and include <functional>) so member pointers and std::reference_wrapper work as expected.
|
|
||
| ## FINISHED | ||
|
|
||
| Completed in [v0.9.3](changelog/v0.9.3.md): |
There was a problem hiding this comment.
This roadmap entry links to changelog/v0.9.3.md, but that file doesn’t exist in docs/changelog/ (latest present is v0.9.2.md). Either add the v0.9.3 changelog file in this PR or change the link/text to reference an existing release to avoid a broken docs link.
| Completed in [v0.9.3](changelog/v0.9.3.md): | |
| Completed in v0.9.3: |
Adds
pred(callable)as a first-class pattern that lifts an arbitrary unary predicate into the match phase, avoiding the need for post-bind guards (pattern[guard]) when no binding is required.Changes
include/ptn/pattern/pred.hpp— Newpred_pattern<Callable>CRTP type +pred()factory. Non-binding (emptybinding_args), consistent withwildcard,any/all, etc.include/ptn/patternia.hpp— Includespred.hpp; exportsptn::pred.CMakeLists.txt— Addspred.hpptoPTN_HEADERS.tests/tests_pred_pattern.cpp— 10 tests: match/fallback, string subjects, composition withany/all, reuse,PTN_ONmacro, fully-qualified usage.tests/CMakeLists.txt— Wires in the new test TU.docs/roadmap.md— Promotespred(callable)from WIP → FINISHED (v0.9.3).Usage