We have the following situation in robotl, where there is a central Sensor.get method op, of which subclasses want to refine the return type:
from effectful.ops.semantics import handler, typeof
from effectful.ops.types import NotHandled, Operation
class AB: ...
class A(AB): ...
class B(AB): ...
class Sensor[T: AB]:
@Operation.define
def get(self) -> T:
raise NotHandled
class ASensor(Sensor[A]):
def get(self) -> A:
return super().get()
class BSensor(Sensor[B]):
def get(self) -> B:
return super().get()
sensors = [ASensor(), BSensor()]
values = {sensors[0]: A(), sensors[1]: B()}
def _sensor_get(sensor):
return values[sensor]
print([typeof(s.get()) for s in sensors]) # [<class '__main__.AB'>, <class '__main__.AB'>]
with handler({Sensor.get: _sensor_get}):
print([typeof(s.get()) for s in sensors]) # [<class '__main__.A'>, <class '__main__.B'>]
I would like the terms in the first print statement to have the specific A, B types.
It's an awkward setup that could be improved by having each type have its own get op, but then all those ops will have to be bound individually. I might go that way. Nevertheless, is there/should there be a way to make this specialization work in effectful?
We have the following situation in robotl, where there is a central
Sensor.getmethod op, of which subclasses want to refine the return type:I would like the terms in the first print statement to have the specific
A, Btypes.It's an awkward setup that could be improved by having each type have its own get op, but then all those ops will have to be bound individually. I might go that way. Nevertheless, is there/should there be a way to make this specialization work in effectful?