Hello,

On Fri, 22 May 2020 21:01:03 +0100
Andrey Cizov <aci...@gmail.com> wrote:

> Sorry I forgot to add the URL:
> https://pypi.org/project/tagged-dataclasses/

How does this compare with many other implementations spread over the
interwebs?

As a quick comment, looks verbose comparing to ML ;-).

For comparison, I found an algebraic2.py in ~/my-python-hacks/, no
dataclasses or particular IDEs involved:

--------------
from collections import namedtuple

# Ideal syntax:
#
#Type = (
#    Int(i: int) |
#    Str(s: str) |
#    Plus(l: Type, r: Type)
#)

def V(name, **args):
    return namedtuple(name, args.keys())


class Union:

    def __init__(self, *variants):
        self.variants = list(variants)

    def add(self, *variants):
        self.variants.extend(variants)
        
    def __instancecheck__(self, inst):
        return isinstance(inst, tuple(self.variants))


def match(val, typ):
    if isinstance(val, typ):
        # Have to return scalar instead of a tuple due to CPython
        # deficiency with := operator
        return val[0]
--------------


Can be used as:

--------------
UnaryExpr = Union(
    Int := V("Int", i=int),
    Str := V("Str", s=str),
)

# Recursive variants should be added after initial definition
UnaryExpr.add(
    Inc := V("Inc", e=UnaryExpr),
    Dec := V("Dec", e=UnaryExpr),
)


def eval(var: UnaryExpr):
    if i := match(var, Int):
        return i
    elif s := match(var, Str):
        return s
    elif e := match(var, Inc):
        return eval(e) + 1
    elif e := match(var, Dec):
        return eval(e) - 1


expr = Dec(Int(123))
print(isinstance(expr, UnaryExpr))
print(eval(expr))
--------------

It's sad "UnaryExpr" (instead of e.g. BinaryExpr) because of a known
deficiency of CPython:

---
$ python3.8 -c "( (a, b) := (1, 2) )"
  File "<string>", line 1
SyntaxError: cannot use assignment expressions with tuple
---

Btw, does anybody know a Python implementation which has this bug
fixed?


> 
> On Fri, 22 May 2020 at 20:25, Andrey Cizov <aci...@gmail.com> wrote:
> 
> > I have developed a library to introduce tagged unions to python
> > that uses dataclasses to define disjoint members of the tagged
> > union (by defining them as Optional fields). With some additional
> > validation I make sure that only one of the fields is not None.
> >
> > I find that it also fits well with the existing analysis tools and
> > IDEs (e.g. PyCharm) and doesn’t require any additional work in
> > order to be supported.
> >
> > I would like to see some criticism and whether that could
> > potentially be a good candidate for python standard library in the
> > future.

[]

-- 
Best regards,
 Paul                          mailto:pmis...@gmail.com
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CVOCMSJYQHTUADG5GEQICVV3X25JLR5R/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to