-
Notifications
You must be signed in to change notification settings - Fork 219
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 overriding singletons (add int + custom type) #990
Comments
Would a solution be to allow overriding a singleton as long as cel.Function("_+_", cel.Overload("_+_decimal", []*cel.Type{TypeDecimal, TypeDecimal}, TypeDecimal),
cel.Overload("_+_decimal_int", []*cel.Type{TypeDecimal, cel.IntType}, TypeDecimal,
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
left, err := toDecimal(lhs)
if err != nil {
return types.NewErr(err.Error())
}
right, err := toDecimal(rhs)
if err != nil {
return types.NewErr(err.Error())
}
return left.Add(right)
}),
),
cel.Overload("_+_int_decimal", []*cel.Type{cel.IntType, TypeDecimal}, TypeDecimal,
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
left, err := toDecimal(lhs)
if err != nil {
return types.NewErr(err.Error())
}
right, err := toDecimal(rhs)
if err != nil {
return types.NewErr(err.Error())
}
return left.Add(right)
}),
),
), |
Hi @theoilie if In terms of cross type support you'll need to type convert to decimal from int, |
Hi @TristonianJones, thanks for the speedy response! To clarify, this custom In other words, on my |
@theoilie CEL doesn't permit type coercion by design, which means that all type conversions are intended to be explicit. CEL will let you do custom additions with custom types, but not across types. The intent is to make it clear that operators should always use CEL semantics, but custom functions may have their own semantics. For example, you can easily support the addition of two Does that make sense? -Tristan |
Thanks for the clarification. That all makes sense, and I'll stick to using explicit conversions / custom functions. For readability of non-technical users, I was hoping to have all ints and doubles implicitly call / be replaced with decimal(int) and decimal(double), respectively, but I ran into some issues trying to wrestle a custom adapter and decorator into accomplishing this. I can respect that adding a configuration option for this request might go against the explicit syntax philosophy of the project, so I'll work around it differently for now and can close out this issue. Thanks again for the speedy support! |
You're welcome. Thanks for reaching out! |
First of all, thanks for all the hard work on this project. It's working great and being very flexible up to this point which is admittedly stretching somewhat far.
Feature request checklist
Change
Following up from this comment, I'm wondering if there's currently a way to override singletons. Specifically, I want to override addition of an int type with a custom fixed-point decimal type (a simple wrapper over https://github.com/cockroachdb/apd).
Example
Create env options with the custom type:
Try to do addition. The final example is the one that doesn't work due to
no such overload
which I believe comes from the int type's Add() function.Alternatives considered
I'm not aware of any other way to allow for syntax like
1 + decimal("1.0")
unless the+
operator on the Int type is overridden. For my use case unfortunately it's not good enough to be able to only dodecimal("1.0") + 1
without allowing the other way around.The text was updated successfully, but these errors were encountered: