classes icon indicating copy to clipboard operation
classes copied to clipboard

Typeclasses with several methods

Open sobolevn opened this issue 5 years ago • 2 comments

There's a common use-case when we need to create a type-class with several methods. Like:

class Functor(Protocol[T]):
    def map(self, function: Callable[[T], Functor[V]]) -> Functor[V]:
        ...

    @classmethod
    def from_value(cls, inner_value: V) -> Functor[V]:
        ...

Currently, there's no way we can do that.

I propose something like this:

@typeclass
class Functor(Protocol[T]):
    def map(self, function: Callable[[T], Functor[V]]) -> Functor[V]:
        ...

    @classmethod
    def from_value(cls, inner_value: V) -> Functor[V]:
        ...

I am not sure how to bound it to the actual implementation.

sobolevn avatar Jun 24 '20 11:06 sobolevn

@Functor.instance
class Maybe(Generic[T]):
    def map(self, function: Callable[[T], Maybe[V]]) -> Maybe[V]:
        ...

    @classmethod
    def from_value(cls, inner_value: V) -> Maybe[V]:
        ...

sobolevn avatar Jun 27 '20 12:06 sobolevn

How is it different from a simple protocol?

sobolevn avatar Jun 27 '20 13:06 sobolevn