Giter Club home page Giter Club logo

adts's Introduction

ADTs for Python

Create algebraic data types (ADTs) easily, using syntax modeled on the enum module.

In simple terms, picture enriching the enum module with support for member classes in addition to constant values.

class Event(ADT):
    QUIT = "quit"

    @dataclass
    class Message:
        msg: str

def process_event(event: Event) -> str:
    match event:
        case Event.QUIT:
            return "quit"
        case Event.Message(msg):
            return f"Message: {msg}"

>>> process_event(Event.Message("test"))
"Message: test"

The focus is on sum types, since product types are already well-served by the language.

Generics are supported

ADTs may also be generic:

from __future__ import annotations

T = TypeVar("T")


class Tree(ADT[T]):
    EMPTY = "empty"

    @dataclass
    class Node:
        left: Tree[T]
        right: Tree[T]

This requires the postponed evaluation of annotations (aka PEP 563), which is activated by importing annotations from __future__.

Class enum members get methods from the enum

A class member will have access to any method on the enum. Since this involves replacing the class with a special subclass, class members must be defined inside the enum.

class MyADT(ADT):
    @dataclass
    class Inner:
        b: int

    def a_method(self) -> int:
        return 1

>>> MyADT.Inner(1).a_method()
1

The differences between Python enums (PEP 435) and ADTs

No mixins

Enums may mix in a class for their members.

class IntEnum(int, Enum):
    A = 1

This adds the mixed-in class to each member's MRO, so isinstance(IntEnum.A, int) holds.

Since ADTs can be heterogenous, no class may be mixed in.

No class getitem

Enums support a class-level __getitem__, which allows fetching enum members by name.

class MyEnum(Enum):
    A = 1

>>> MyEnum['A']
<MyEnum.A: 1>

Since ADTs need to be able to be generic, this syntax conflicts with parametrizing a generic ADT, so it is not supported.

T = TypeVar("T")
E = TypeVar("E", bound=BaseException)

class Result(ADT[T, E]):
    @dataclass
    class Ok:
        val: T

    @dataclass
    class Err:
        err: E

adts's People

Contributors

hack-parthsharma avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.