Skip to main content

type unions

Project description

Type Unions

typeunions is a library providing algebraic data types in Python, with a clean and intuitive syntax that use all the strength of match and dataclass in python 3.10

Install

pip install typeunions

Usage

@typeunion
class Message
    Quit: ()
    Move: { 'x': int, 'y': int }
    Write: (str)
    ChangeColor: (int, int, int)

Each types (Quit, Move, Write, ChangeColor) is a python dataclass. Hence you can use the match case pattern:

message = Message.ChangeColor(255, 128, 0)

match message:
    case Quit: print("quit")
    case Move(x, y): print(f"move {x} {y}")
    case Write(s): print(s)
    case ChangeColor(r, g, b): print(f"{r}{g}{b}")
    case _: print("error")

Here is an exemple of the same code with rust enum

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}
match message {
    Quit => println!("quit")
    Move(x, y) => println!("move {} {}", x, y)
    Write(s) => println!(s)
    ChangeColor(r, g, b) => println!(f"{}{}{}", r, g, b)
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typeunions-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typeunions-0.0.1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file typeunions-0.0.1.tar.gz.

File metadata

  • Download URL: typeunions-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for typeunions-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7a9cc4d7eff3ec4e542530b9774acbcc2adfab1a9b43f08603c4f64f6a8e57f8
MD5 8aead478aa76d13001894f8350cd9efa
BLAKE2b-256 e0edbdc0ef6161ac757e130a018d85dfb3463fb652eacf8437a1a4b6f8e36913

See more details on using hashes here.

File details

Details for the file typeunions-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: typeunions-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for typeunions-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4723ebb4177de76532d5a70af51d6db6550bcff78a314a51ce2918c11de1657b
MD5 de36c6e720d667a59418f81e3b4044da
BLAKE2b-256 2ad1e40183a32cf484d167d551cafcc6c2a0b897055faed26397970f0d5b08cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page