4 ms·
I like the line in the Zen of Python, "Explicit is better than implicit." You should get a type error if you try to use an int in boolean logic. I hate auto ty
by djsumdog 9y ago
I like the line in the Zen of Python, "Explicit is better than implicit."
You should get a type error if you try to use an int in boolean logic. I hate auto type-conversion. Autoboxing in Java is kinda nice, but really the base types should have been objects to begin with, and operator overloading should have been allowed.
(Although I do admit operators in Scala get confusing as hell. I like one of the recent RFCs that would require every Scala operator to have an actual "word" function alias to go with it so you can tell what the hell it does).
- Y_Y 9y agoBut i assume you're ok with upcasting an int to a double. That is, unless you make the Haskell Prelude. There is a real and useful sense in which booleans are a subtype of integers and i think its not unreasonable to automatically cast between them. Can't say if it's a good idea or not though.
- Twisol 9y agoThere is a real and useful sense in which _everything_ is an integer; see Godel numbering. There's also a real and (debatably) useful sense in mathematics in which everything is a set. Even still, we prefer to impose structure and convention because it helps us understand what's going on, by restricting the kinds of things we need to be able to expect. Booleans are the same kind of thing. We don't normally do arithmetic on them; at most we might treat `bool` like Z-mod-2. But Z-mod-2 behaves very differently from `int`.
- Y_Y 9y agoI'd love to hear of a case where anyone has actually computed a Goedel number. We very often do arithmetic on booleans, the a canonical homomorphism (Iverson bracket) with 1 ~ True and 0 ~ False where && and || are multiplication and addition. But I guess you knew that.
- yorwba 9y agoThat is not the only homomorphism. You could just as well have 0 ~ True and 1 ~ False, where && and || are addition and multiplication. Booleans don't have enough algebraic structure to nail down a unique embedding. Integers and reals don't have the same problem, because there's a unique ring homomorphism between them.
- catnaroek 9y agoBlessing noncanonical structures and mappings is indeed evil. Let's take this to its logical conclusion and banish type classes.
- yorwba 9y agoI'm not sure what you mean by that. Type classes are all about making canonical structures explicit.
- catnaroek 9y agoType class instances are only “canonical” by fiat. Once you elaborate type classes into record-passing, by no means are the resulting instance types uniquely inhabited. This is just as bad as the existence of truthy or falsy values.
- ubernostrum 9y agoYou should get a type error if you try to use an int in boolean logic. So define: def __bool__(self): raise TypeError("I don't like non-bool booleans") on your classes, and you're good to go. Python isn't doing type conversion, it's doing operator overloading and letting you define how your class should work with the 'if' statement. It won't help you with the already-defined behavior of built-in and third-party classes, but you can have all your own code be unusable in boolean operations if you like.