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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve get functions for patterns #1292

Merged
merged 3 commits into from
Jan 15, 2025
Merged

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Jan 15, 2025

This is one tiny change I wanted to implement for a while. Im WMA, if you want to implement a rule that returns the same expression if certain test fails, the way to do it is

expression: Half[expr]:=If[NumberQ[expr], expr/2, expression]

i.e. use a Pattern[patname, pat] expression to pass the whole original unevaluated expression (Half[a] in the example) to the replace expression. However, we could not do this in the patterns especified in docstrings, because we "overloaded" the meaning of patname: pattern in the function that loads eval_* and format_* methods: we use them to indicate in the case of format_ methods to which Format corresponds the rule.
As in some methods we need the full original expression, then we added a "hack" to interpret a parameter expression in an eval_ function to contain the whole expression.

This PR removes the need of all this hackiness by changing the convention in format_ methods docstrings: if you want to especify an specific Format, you can do it with a docstring of the form (form1, form2,...,): exprpattern which is not a valid WL expression.

We can also use this form to avoid the use of Optional in the eval_ methods, so each eval_ method could return always a BaseExpression. But this will come later.

``expr: MyFuction[x_,y_]``. To specify a format in a `format_`
method docstring, we use now the syntax ``(MyForm,): MyExpr[...]``.
@rocky
Copy link
Member

rocky commented Jan 15, 2025

LGTM

@mmatera mmatera merged commit ae28014 into master Jan 15, 2025
14 checks passed
@mmatera mmatera deleted the improve_get_functions_for_patterns branch January 15, 2025 20:15
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.

2 participants