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

Allow override core operators #455

Closed
cnogueira opened this issue Jul 22, 2019 · 3 comments · Fixed by #456
Closed

Allow override core operators #455

cnogueira opened this issue Jul 22, 2019 · 3 comments · Fixed by #456
Milestone

Comments

@cnogueira
Copy link
Contributor

As of right now operator overrides are disabled. This means that one cannot change the behaviour of operators like == or any others.

While the use cases might be very marginal I do have a use case for this: I need to make == operator less strict on the types of the arguments. e.g.

//  default behavior
1 == "1" // false

// desired behavior
1 == "1" // true

Problem is that by design everything can be extended or overridden but the unary & binary operators.

My idea is to add a flag to enable the operator overrides. So users can override core operators if they want to. i.e.

@Override
  public List<BinaryOperator> getBinaryOperators() {
    List<BinaryOperator> operators = new ArrayList<>();

    operators.add(new BinaryOperatorImpl("==", 30, CustomEqualsExpression.class, Associativity.LEFT));

    return operators;
  }

where CustomEqualsExpression.class would be the override for the == core operator.

@slandelle
Copy link
Contributor

IMHO, Pebble is a Java library and core operators should stick to Java semantics. Why not convert to String or Integer, or compare to correct value or write your own custom extension ?

@cnogueira
Copy link
Contributor Author

Our application contains a live HTML editor which uses pebble behind the scenes to provide templating functionality.

we have some monetary amounts which are already localized (hence the string type) and we'd really want to enable our users to do stuff like {% if amount > 10 %}.

for us it's not acceptable to expect our users to know which placeholders contain localized amounts (i.e. strings) and which contain actual numbers (i.e. index, counts, etc)

I know it's a fairly edgy use case, but having this will really improve our users experience.

@ebussieres
Copy link
Member

One of core feature if the extensibility of the language. I think it's a good addition to pebble. I'll take a look at the PR later this week.

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 a pull request may close this issue.

3 participants