<div dir="ltr"><div><div><div><div>There seems to be a movement (inspired by Haskell no doubt) that argues that sum types are the next fashion in language design. E.g. Googling for "sum types python" I found <a href="https://chadaustin.me/2015/07/sum-types/">https://chadaustin.me/2015/07/sum-types/</a> (I should look up the author, he works a floor away from me :-).<br><br>If you read that carefully, a sum type is mostly syntactic sugar for a tagged union. They're easy enough to add to compiled languages. I find it interesting that Austin mentions several types that the "Maybe" type is equivalent to the convention of returning NULL/null/None, and he uses it as a simple example (the simplest, in fact) of a sum type. So in PEP 484 that's called Optional[X], and indeed Optional[X] is syntactic sugar for Union[X, None], and of course unions in Python are always tagged (since you can always introspect the object type).<br><br>So Python's equivalent for Sum types seems to be PEP 484's Union types. For matching, a sequence of isinstance() checks doesn't look too shabby:<br><br></div>def add1(a: Union[int, str, bytes]) -> Union[int, str, bytes]:<br></div><div> if isinstance(a, int):<br></div><div> # In this block, the type of a is int<br></div><div> return a+1<br></div><div> if isinstance(a, str):<br></div><div> # Here a is a str<br></div><div> return a + 'x'<br></div><div> # In this block, the type of a is bytes (because it can't be anything else!)<br></div><div> return a + b'x'<br></div><br></div>Of course in this specific example, using a constrained type variable (similar to AnyStr) would be much better, since it declares that the return type in this case is the same as the argument type. However that's not what Sum types do in Haskell either, so we can't really blame them for this -- in Haskell as in PEP 484, Sum types and generics are pretty much orthogonal concepts. I think Sum/Union shine when they are either used for the argument (so the function must have a list of cases) or for the return value so the caller must have a list of cases), but not for both.<br><br></div><div>That's not to say that it wouldn't be an interesting exercise to see if we can add a nice pattern matching syntax to Python. Austin claims that one of the advantages of Sum types in Haskell is that it supports a recursive matching syntax. It would probably look different than it looks in compiled languages though, because a Python compiler doesn't know much about the types occurring at runtime, so e.g. it couldn't tell you when you're missing a case. However, a type checker could -- this might be the first new Python feature that was in some sense enabled by PEP 484.<br></div><div><br></div>(And here I thought I was off-topic for the thread, but this thread was actually started specifically to discuss Sum types, so now I feel better about this post.)<br><br>PS. I recall vividly the classes in category theory I took in college. They were formative in my education -- I knew for sure then that I would never be a mathematician.<br><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 15, 2016 at 4:37 PM, Greg Ewing <span dir="ltr"><<a href="mailto:greg.ewing@canterbury.ac.nz" target="_blank">greg.ewing@canterbury.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Koos Zevenhoven wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Maybe you mean to propose that the concept of sum types should be<br>
introduced in typing/mypy.<br>
</blockquote>
<br></span>
I'm not deeply into category theory, but the proposal<br>
seems to be that Sum would be a special kind of type<br>
that's assumed to be the same type wherever it appears<br>
in the signature of a function.<br>
<br>
That might work for the special case of a function of<br>
one parameter that returns the same type as its<br>
argument, but that's about all.<br>
<br>
As has been pointed out, type parameters allow the<br>
same thing to be expressed, and a lot more besides,<br>
so a Sum type is not needed. We already have everything<br>
we neeed.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Greg</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>