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

Support custom overloads for standard operators. #252

Closed
2 of 3 tasks
TristonianJones opened this issue Aug 26, 2019 · 6 comments
Closed
2 of 3 tasks

Support custom overloads for standard operators. #252

TristonianJones opened this issue Aug 26, 2019 · 6 comments
Assignees
Labels
enhancement New feature or request P2
Milestone

Comments

@TristonianJones
Copy link
Collaborator

TristonianJones commented Aug 26, 2019

Feature request checklist

  • There are no issues that match the desired change
  • The change is large enough it can't be addressed with a simple Pull Request
  • If this is a bug, please file a Bug Report.

Change

The CEL standard environment declares function overloads for operators and built-in functions such as the type-conversion function timestamp(<val>). To assist with extensions to the built-in operators and helper functions, it should be possible to add an overload to the existing set without overriding all overloads.

Example

Desired behavior, add a new overload to an existing function:

cel.Declarations(
	decls.NewFunction('timestamp',
		decls.NewOverload("custom_to_timestamp",
			[]*exprpb.Type{decls.NewObjectType("custom")}, decls.Timestamp)),
)

Current behavior, replace the existing function with all current functions plus the custom one:

cel.Declarations(
	decls.NewFunction('timestamp',
		decls.NewOverload(overloads.TimestampToTimestamp,
			[]*exprpb.Type{decls.Timestamp}, decls.Timestamp),
		decls.NewOverload(overloads.StringToTimestamp,
			[]*exprpb.Type{decls.String}, decls.Timestamp),
		decls.NewOverload(overloads.IntToTimestamp,
			[]*exprpb.Type{decls.Int}, decls.Timestamp),
		decls.NewOverload("custom_to_timestamp",
			[]*exprpb.Type{decls.NewObjectType("custom")}, decls.Timestamp)),
)

** Alternative Considered **

Alternatively, such overloads could be added at a function level, though the cel.Declarations functional option is the preferred way to specify declarations and there really shouldn't be two ways to do the same thing.

A refinement on the approach would be to expose helper functions for creating the new functions and overloads.

cel.Declarations(
  cel.Fn('timestamp').Overload(...),
)
@TristonianJones TristonianJones added enhancement New feature or request P2 labels Sep 9, 2019
@TristonianJones
Copy link
Collaborator Author

The custom overloads are actually supported. The issue is actually in how they're resolved across declaration scopes.

@TristonianJones TristonianJones self-assigned this Feb 11, 2021
@TristonianJones TristonianJones added this to the CEL v0.8.0 milestone Feb 11, 2021
@hmcty
Copy link

hmcty commented Jun 29, 2021

Any updates on this? I'm attempting to overload the 'add' operator however it's giving me an overlapping overloads error.

@TristonianJones
Copy link
Collaborator Author

No updates at this time, other priorities have come up that need attention, but I hope to be able to focus on this again in the future.

@rafalbigaj
Copy link

The interest for this feature is growing 😄

@TristonianJones
Copy link
Collaborator Author

It would actually appear as though this feature is working just fine from the declaration side, but is problematic from the runtime function registration side. This issue is closed and work toward fixes for evaluation dispatch will take place in #493.

@dmateusp
Copy link

Is there documentation around overloading standard operators?
I have been trying to overload the _+_, haven't been able to.

This comment seemed to indicate some changes were needed for the standard operators

This has been updated for all extension functions, but has yet to be implemented for all of the standard operators, including type conversion methods which I think are essential for ensuring that non-standard types can be converted into standard ones.

#493 (comment)

Then there's this PR #747 which has this in the description:

Operators such as +, -, ==, etc. do not support extension as they are marked as singleton
implementations; however, most human-readable overloads such as contains support extension.

Is it still impossible to overload + and other operators?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P2
Projects
None yet
Development

No branches or pull requests

4 participants