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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue May 17 05:38:10 EDT 2016


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.

-- 
Greg


More information about the Python-ideas mailing list