[Python-3000] Type annotations: annotating generators
Kay Schluehr
kay.schluehr at gmx.net
Fri May 19 08:02:49 CEST 2006
Guido van Rossum schrieb:
>> Another question: is there any intention to
>> support ML style quards for pattern matching on algebraic /recursive
>> types?
>
>
> Can you rephrase that without references to ML or quads?
No, but the basic idea is easily explained using ML syntax and semantics.
ML lets users define their own datatypes which has the obvious advantage
that they do not need to pollute their function signatures with
definitions. Python would likely use class syntax for this purpose or
interface syntax in future (?)
Some examples:
(* simple 'algebraic datatype' created by RED, YELLOW, BLUE . The vertical bars mark alternatives. *)
datatype PrimaryColours = RED | YELLOW | BLUE;
(* simple recursive datatype. The '*' token is used to combine types to a cartesian product type *)
datatype IntTree = NIL | NODE of int * IntTree * IntTree;
There is a symmetry between the usage of the vertical bars in datatype
definitions and in "pattern matching" where the pattern mentioned here
refers to the "algebraic datatype".
(* Function 'colourString' that keeps a PrimaryColours type. Note that ML is statically typed and each type will
be inferred so declarations in function signatures can be omitted.
*)
fun colourString c =
case c of
RED => "red"
| BLUE => "blue"
| YELLOW => "yellow"
;
(* Another function defined on IntTree *)
fun preOrder t =
case t of
NIL => [ ]
| NODE (i,left,right) => (preOrder left) @ (preOrder right) @ [i]
;
Regards,
Kay
More information about the Python-3000
mailing list