Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

so now when you have a function that takes in a Car object, you have no idea what fields those objects might have, because it's all optional! Which means the checks for the validity of each field end up spreading out to every function.


> so now when you have a function that takes in a Car object, you have no idea what fields those objects might have, because it's all optional!

Your types are already optional if you're adding constructors for each permutation of all input parameters.


Which is no worse than the situation in a dynamically typed language where every field in every object could be optional.

Dynamic typing advocates sometimes miss that statically typed languages don't force you to encode every invariant in the type system, just those that seem important enough.

Or, if you really want to go overboard, you could use a dependently typed language and write functions that only accept cars with a specific combination of fields not being empty. But that's typically not worth the complexity.


Frankly, your contract was that you have no idea what fields those objects might have. I'm just fulfilling it. You won't have checks for validity of each field, as Optional is valid, but you will have to have code that handles Optional<> types (so things like foo.getModel().orElse()...), which is the requirement you described. That doesn't mean you'll be constantly checking the validity of each field.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: