Skip to content

Commit aa5f5f2

Browse files
Add __add__ to String which joins and fix bug allowing preserved binary methods
1 parent 7aae8af commit aa5f5f2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

python/egglog/builtins.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def value(self) -> str:
103103
@method(egg_fn="replace")
104104
def replace(self, old: StringLike, new: StringLike) -> String: ...
105105

106+
@method(preserve=True)
107+
def __add__(self, other: StringLike) -> String:
108+
return join(self, other)
109+
106110

107111
StringLike: TypeAlias = String | str
108112

python/egglog/runtime.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,22 @@ def _defined_method(self: RuntimeExpr, *args, __name: str = name, **kwargs):
635635

636636

637637
for name, r_method in itertools.product(NUMERIC_BINARY_METHODS, (False, True)):
638+
method_name = f"__r{name[2:]}" if r_method else name
638639

639-
def _numeric_binary_method(self: object, other: object, name: str = name, r_method: bool = r_method) -> object:
640+
def _numeric_binary_method(
641+
self: object, other: object, name: str = name, r_method: bool = r_method, method_name: str = method_name
642+
) -> object:
640643
"""
641644
Implements numeric binary operations.
642645
643646
Tries to find the minimum cost conversion of either the LHS or the RHS, by finding all methods with either
644647
the LHS or the RHS as exactly the right type and then upcasting the other to that type.
645648
"""
649+
# First check if we have a preserved method for this:
650+
if isinstance(self, RuntimeExpr) and (
651+
(preserved_method := self.__egg_class_decl__.preserved_methods.get(method_name)) is not None
652+
):
653+
return preserved_method.__get__(self)(other)
646654
# 1. switch if reversed method
647655
if r_method:
648656
self, other = other, self
@@ -668,7 +676,6 @@ def _numeric_binary_method(self: object, other: object, name: str = name, r_meth
668676
fn = RuntimeFunction(Thunk.value(self.__egg_decls__), Thunk.value(method_ref), self)
669677
return fn(other)
670678

671-
method_name = f"__r{name[2:]}" if r_method else name
672679
setattr(RuntimeExpr, method_name, _numeric_binary_method)
673680

674681

0 commit comments

Comments
 (0)