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

Who needs type safety when we got integers.


Is this just using some vague connection to ride on one of your favorite hobby horses? Or does this have a connection to the article, and I missed it?


The problem is that C and POSXIX don't (idiomatically) provide rich enough data types force checking the error condition. The fact that the error is signaled by a random integer (-1) is horrible. In a language with stronger types and richer data structures, one can have a return type that is a disjunction of {failure, parent, child}, so you can never accidentally treat a failure as a PID.

In a functional language this would look like a datatype (essentially a generalized enum), while an OO language you would use different subclasses of a common superclass.

In C you can return a tagged union and check the tag, but nothing forces you to do the check. A user of this API can just go ahead and assume the success branch of the union. Furthermore, this isn't idiomatic POSIX, so it is never done.

[edit: "subclass" -> "superclass"]


You could return a pointer or null. That would successfully force the "did it succeed" check, but of course raises questions about memory management.

Tagged union is probably the best approach. It doesn't prevent skipping the check, but it at least makes "thing I am supposed to use" different than "thing I am supposed to check".


Raising an exception would also work.


well you're no fun.


Who even wants that? When I am coding, sure as hell some compiler vendor won't be telling me what to use my bits for!

I identify myself as an atypical coder.


If you tell yourself what the bits are for and stick to it, that is still type. Real chaos is when even you don't.




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

Search: