3 ms·
Yes that would be a bad way to do structs in Haskell. Instead you would simply use Haskell's record syntax, ie. data Person = Person { firstName :: String, last
by samungo 11y ago
Yes that would be a bad way to do structs in Haskell. Instead you would simply use Haskell's record syntax, ie. data Person = Person { firstName :: String, lastName :: String, age :: Int }
You could also easily define more specific types like for example: type Age = Int, which should prevent somebody from creating a person where they use a zip code for that person's age.
- mightybyte 11y agoI'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.