Skip to main content

Union types for python

Project description

``uniontypes 0.1``
============

|build status|

Union types For python.

These appear other languages like Haskell (``Either``) and
Scala (``Union`` or ``\/``).

This extends to any arbirary set of types.


Examples
--------
Creating a union type
~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python

>>> from uniontypes import Union
>>> u = Union[list, tuple, str]
>>> u
<class 'uniontypes.Union[list, tuple, str]'>

Creating boxed values
~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python

>>> u([1, 2, 3])
Union[list, tuple, str][list] [1, 2, 3]
>>> u((1, 2, 3))
Union[list, tuple, str][tuple] (1, 2, 3)
>>> u('123')
Union[list, tuple, str][str] '123'

Accessing the inner wrapper types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python

>>> u[list]
<class 'uniontypes.Union[list, tuple, str][list]'>
>>> u[tuple]
<class 'uniontypes.Union[list, tuple, str][tuple]'>
>>> u[str]
<class 'uniontypes.Union[list, tuple, str][str]'>

Class heirarchy
~~~~~~~~~~~~~~~
.. code-block:: python

>>> isinstance(u([1, 2, 3]), u)
True
>>> isinstance(u([1, 2, 3]), u[list])
True
>>> isinstance(u([1, 2, 3]), (u[tuple], u[str]))
False

Option Types
------------
.. code-block:: python

>>> from uniontypes import Option
>>> oint = Option[int]
>>> oint
<class 'uniontypes.Option[int]'>
>>> oint(1)
Option[int] 1
>>> oint(None)
Nothing
>>> oint(None) is oint.nothing
True


.. |build status| image:: https://travis-ci.org/llllllllll/uniontypes.svg?branch=master
:target: https://travis-ci.org/llllllllll/uniontypes

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

uniontypes-0.1.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Supported by

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