Make the Type System Do the Work
nathan.ca
Make the Type System Do the Work
1–10 of 141 posts
Re: Make the Type System Do the Work
#2> The only problem is that you end up with a variable that fails to describe itself better than “I’m a number”.
Our application servers need to know about updated entities, so we broadcast notifications when an entity has been updated - but we prefer to leave it up to the individual applications to retrieve the updated entity as they see fit.
So we're often dealing with numerical ids of entities, and we've had a few bugs arise from code like so
public void something(long accountId, long websiteId)
When people have accidentally used the wrong long in the wrong place. So we've now moved to using simple wrapper types around aforementioned longs - public void something(AccountId accountId, WebsiteId websiteId)
It does come at a little bit of cost as we're passing these things around a messaging system, so now we have to think about how they are serialized (Java serialization is problematic, and there are occasional religious wars about GPB vs. JSON), but it leverages the type system to make it every explicit what this number represents.Re: Make the Type System Do the Work
#3Sorry, what language are these example given in?
Re: Make the Type System Do the Work
#4Sorry, what language are these example given in?
[deleted]
Re: Make the Type System Do the Work
#5Sorry, what language are these example given in?
[deleted]
Re: Make the Type System Do the Work
#6The Boost.Units[1] library takes this even further, allowing you to do things like divide a length by a time quantity and get a velocity, again with compile-time type checking.
[1]: http://www.boost.org/doc/libs/1_55_0/doc/html/boost_units.ht...
Re: Make the Type System Do the Work
#7Sorry, what language are these example given in?
C++
Re: Make the Type System Do the Work
#8Sorry, what language are these example given in?
[deleted]
Re: Make the Type System Do the Work
#9To take this even further, look at OCaml's strong, inferred typing.
Re: Make the Type System Do the Work
#10To take this to the current state of the art, look at Haskell, Agda, and Idris. Latter two being dependently typed.
Steps for learning Haskell: