2 ms·
You can write very safe code by using structs (even 1-element structs) for everything. But there's no generics system and very little in the way of safe tools f
by lmm 12y ago
You can write very safe code by using structs (even 1-element structs) for everything. But there's no generics system and very little in the way of safe tools for making one; writing a safe "generics runtime" and macros to use it puts you halfway to writing a new language.
- proveanegative 12y ago>structs (even 1-element structs) for everything. Are there any large C projects written in this style?
- lmm 12y agoAll I'm aware of is a friend's private project of around 20KLOC.
- dllthomas 12y agoI don't recall the LOC, but I used this style at a recent position. Basically, the idea is not to use bare primitive types anywhere - instead wrapping them in something semantic. Probably the two biggest wins from this approach were distinguishing types of indexes and (in the particular project) distinguishing price from quantity. It does add a bit of boilerplate. For the indexes in particular, foo_lookup(foo, idx) is substantially safer than foo[idx.value] - but then you've gotta write the lookup functions (which may just be a macro but is still a bit of cruft). Locally, if using var.value everywhere in math is getting ugly, you can of course pull things out into temporary primitive variables - enforcing things at the boundaries between functions provides almost all of the benefit.
- tel 12y agoI would love to see even small examples of this.
- deleted 12y ago[deleted]