4 ms·
I'll second this comment. No self respecting Haskell programmer would use the tuple or dictionary scheme described. This is exactly how you'd expect to see it
by mightybyte 11y ago
I'll second this comment. No self respecting Haskell programmer would use the tuple or dictionary scheme described. This is exactly how you'd expect to see it done in Haskell.
data Person = Person
{ firstName :: String
, lastName :: String
, age :: Int
}
The compiler automatically creates the following functions for you.
Person :: String -> String -> Int -> Person
firstName :: Person -> String
lastName :: Person -> String
age :: Person -> Int
So you get all the expected type safety. The nice thing is that since this syntax is very light, you end up creating all kinds of small types here and there for better type safety. Whenever I find myself using tuples of more than 3 or so items, I'll frequently create a new type for it. This serves as better documentation and makes the code easier to refactor.