4 ms·
> The C compiler picks up on that, too. Except when it's not. Just five days ago I was debugging an error in my Erlang port driver that was caused by me passin
by dozzie 9y ago
> The C compiler picks up on that, too.
Except when it's not. Just five days ago I was debugging an error in my Erlang
port driver that was caused by me passing receiver (ErlDrvTerm, an int in
disguise) in the place where I wanted number of iterations. The funnier thing
was that the declaration of the function had the arguments in correct order
(and that's what guided me), but definition had them swapped. The compiler did
not catch that bug, because, well, both are ints, so apparently the
declaration and definition match, don't they?
- comex 9y agoThat’s why people should use one-element structs instead of typedefs for that kind of use case. A struct is a distinct type that can’t be accidentally mixed up with random integers, but its memory representation and efficiency will generally be identical; and you can add one-liner conversion functions to minimize syntactic overhead when you do need to convert to/from raw integers. Same idea as ‘newtype’ in Haskell; it works pretty much just as well in C, at least for ‘ID number’ sorts of types where you’re usually just shuttling values from place to place rather than doing any arithmetic. (For types where you do need arithmetic, it gets pretty ugly in languages without operator overloading. Except Go, which doesn’t have operator overloading but does have builtin support for defining distinct versions of integer types.)
- yawaramin 9y agoIn fact, make the structs opaque so that you retain full control over the data and its invariants.