4 ms·
it's quite rare that I've encountered a type error in a Python program I hear that relatively frequently from people who consider types to be things like funct
by chadaustin 11y ago
it's quite rare that I've encountered a type error in a Python program
I hear that relatively frequently from people who consider types to be things like functions, strings, integers, and other "representation types". But the benefit of type systems, especially type systems as expressive as Haskell, is that you, many times, can encode your logical invariants in the type system by introducing new nominal types. For example, you can introduce a new type for each type of ID in your system, like CustomerID, ExperimentID, EventID, which prevents you from mixing those up. You can use types to distinguish between mutable and immutable data structures. It's perfectly okay to use object identity when comparing immutable data structures, but it's usually a bug if the data structure is mutable.
When you take a more expansive view of what type systems can offer, they're more useful.
- jdmichal 11y ago> It's perfectly okay to use object identity when comparing immutable data structures, but it's usually a bug if the data structure is mutable. I don't understand this statement. The only thing immutability provides here is the ability to know that an object didn't change by knowing that it's the same object. There's no guarantee of value equality from identity unless all objects are interned.
- chadaustin 11y agoAh, yeah, perhaps I should have elaborated more. It's a subtle issue, but it's one that comes up all the time when optimizing React by removing unnecessary renders. With immutable data structures, you know that x == y implies, even if x was initialized a while ago, that the data is the same. With mutable data, that's not true -- perhaps the values referenced by x have changed value in the meantime, so x == y does not imply that the data is the same.
- agentultra 11y agoEncoding an invariant in the type system is definitely a strong benefit of Haskell and other similar languages. You can do more than simple identity/immutability with types. I find the flexibility of the type system to share some similar properties with C++'s more advanced features. Used sparingly they can be quite powerful but come with a complexity that few mortals will be able to comprehend. In a distributed system, for example, you can encode an invariant in the type system that will constrain the execution to only valid states. This is immensely powerful. However you can also model arbitrarily complex computations into the type system which will make sense to no one but the author who wrote it. One thing I do miss in Python that a type system gives you is the compiler's ability to do exhaustive pattern matching. Forcing you to deal with every case explicitly is a huge advantage. In fact, I'm currently working on a formal specification of eventlet's Semaphore class as an exercise (I'm in the process of learning) as we've found just such a bug in a large Python code base where a particular exception is not caught and locks up the whole system. An exhaustive pattern search would have forced the authors to deal with that exception and this bug wouldn't have surfaced.