Skip to content

Specialization of method operation signatures in subclasses #749

Description

@cooijmanstim

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions