News for hackers. Stuff that matters.
Re:Diminishing returns of static typing by comex on Monday October 02, 2017 @11:20PM (#15389863)
That’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.)
Re:Diminishing returns of static typing by yawaramin on Tuesday October 03, 2017 @12:36AM (#15390189)
In fact, make the structs opaque so that you retain full control over the data and its invariants.