[Python-ideas] "Sum" Type hinting [was: Type hinting for path-related functions]

Guido van Rossum guido at python.org
Tue May 17 11:59:15 EDT 2016

On Tue, May 17, 2016 at 2:38 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>> Hmm... In Java there is a form of syntactic sugar that automatically deals
>> with such boxes called auto-(un)boxing, IIUC. So I still think it can be
>> called syntactic sugar.
> That's not the same thing either. Boxing in Java is a hack
> to make up for the fact that some types are not objects,
> and the auto boxing and unboxing is there so that you can
> forget about the boxes and pretend that e.g. int and Integer
> are the same type (at least for some purposes).
> But with algebraic types, the boxes are entities in their
> own right whose presence conveys information. You can have
> more than one kind of box with the same contents:
> data Box = Matchbox Int | Shoebox Int
> Not only is a Matchbox distinguishable from a Shoebox at
> run time, but Box is a distinct type from Int -- you can't
> pass an Int directly to something expecting a Box or
> vice versa.
> A realisation of algebraic types in Python (or any other
> language, for that matter) would require carrying information
> at run time about which kind of box is present. This is
> in contrast to a union type, which is purely a compile-time
> concept and has no run-time implications.

I'm sorry, I wasn't trying to claim that Java's auto-(un)boxing was
anything like ADTs; I know better. I was just quipping that just
because there's a difference between a box containing a piece of fruit
and the piece of fruit itself, that doesn't mean handling the box
can't be considered syntactic sugar -- your original remark claimed
something wasn't syntactic sugar because of the difference between the
box and its contents, and that's what I disagree with.

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list