Python from Wise Guy's Viewpoint
Dirk Thierbach
dthierbach at gmx.de
Sun Nov 2 05:38:23 EST 2003
Lex Spoon <lex at cc.gatech.edu> wrote:
> To add to the situation, HM flags extra errors, too, that many people
> would not consider "type errors" but which are for HM's purposes. For
> example, it is considered a type error if two branches of an "if" do
> not match, even if one branch is impossible or if the later code can
> remember which branch was followed.
[...]
> val (tag, thingie) =
> if (whatever)
> then (0, 1)
> else (1, 1.0)
>
> val myotherstuff =
> if tag = 0
> then (tofloat thingie) + 1.5
> else thingie + 1.5
The point here is of course that you "glue together" the tag
and the value, with the additional side effect that this documents
your intention. So you would write in this case
data Thingie = Tag0 Integer | Tag1 Float
and then you can write
myfirststuff whatever = if whatever then Tag0 1 else Tag1 1.0
myotherstuff (Tag0 thingie) = (fromInteger thingie) + 1.5
myotherstuff (Tag1 thingie) = thingie + 1.5
Then the type checker will happily infer that
myfirststuff :: Bool -> Thingie and
myotherstuff :: Thingie -> Float
So you indeed have to express your tags a bit differently. Is this
asking too much? Is that so inconvient, especially when you get a
good documention of your intentions for free?
- Dirk
More information about the Python-list
mailing list