跳转到主要内容

Python的联合类型

项目描述

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

|构建状态|

Python的联合类型。

这类似于Haskell(``Either``)和
Scala(``Union``或``\/``)等语言。

它可以扩展到任意类型的集合。


示例
--------
创建联合类型
~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python

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

创建封装值
~~~~~~~~~~~~~~~~~~~~~
.. 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'

访问内部包装类型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. 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]'>

类层次结构
~~~~~~~~~~~~~~~
.. 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

选项类型
------------
.. 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


.. |构建状态| image:: https://travis-ci.org/llllllllll/uniontypes.svg?branch=master
:目标:https://travis-ci.org/llllllllll/uniontypes

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

uniontypes-0.1.0.tar.gz (3.6 kB 查看哈希值)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面