feat: Enable casting to native types and obtaining backend-native functions #135
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
While working on several features, we noticed that there are some limitations to the backend interface, namely:
serialize
method that always returns string, and there isvalue_of
method that either returns a number ORNone
if it is not possible to convert an expression to number. There is no single function that would convert expressions representing numbers to numbers, and other expressions to strings.backend.as_expression
, e.g.:backend.as_expression(f"sin({backend.serialize(some_expr)})")
which a) is not very readable , b) incurs a performance penalty ifsome_expr
is long and complicated.This PR solves both of this issues by defining
backend.as_native(expr)
andbackend.func(func_name)
methods.The
backend.func
can be used as follows:The name
func_name
was chosen instead of e.g.backend.function
so that we have more concise notation while still maintining a reasonable level of readability.Please verify that you have completed the following steps