Hacker News new | ask | show | jobs
by isaacabraham79 2080 days ago
No. In languages like F#, you can encode in the type system that the result can be either a customer or an error. And the compiler won't let you "access" the customer until you've safely checked that the result is, indeed, the customer rather than an error.

In this way you end up encoding more intent within your types and behaviours rather than implicitly letting the consumer figure it out themselves.

2 comments

> the compiler won't let you "access" the customer until you've safely checked that the result is, indeed, the customer rather than an error.

/u/anw's example shows that, in fact, the benefits of the type system are greater than that. Not only can the type system prevent you from accessing data before confirming that the data is present (which is, at the end of the day, not much better than an Optional type, already available in Java and others), but it can also prompt you to "handle" all possible cases.

Adding to this, it's nice to have the compiler enforce these things rather than rely on the dev to see the variable name and treat it accordingly. In the latter case, you're one sleepy afternoon away from having things blow up in production because you "thought there was no way that that variable could be a null pointer"