4 ms·
For something like this, you don't need an advanced type system. Even C can do struct duration { int in_nanoseconds; }; struct duration seconds_duration (i
by mooism2 13y ago
For something like this, you don't need an advanced type system. Even C can do
struct duration { int in_nanoseconds; };
struct duration seconds_duration (int in_seconds) {
struct duration dt;
dt.in_nanoseconds = in_seconds * 1000000000;
return dt;
}
/*TODO: milliseconds_duration(), microseconds_duration(), etc */
- dllthomas 13y agoI highly recommend doing this kind of thing, incidentally. Just because two things have the same backing representation does not mean they're of the same type, and wrapping in a struct tells the compiler to keep them separate. It doesn't have any impact on performance; C structs don't carry anything more than their member and you've only got the one member. I do this with indexes and ids as well.
- gohrt 13y agoOne would hope that 'typedef' would define a type in C, protecting the programmer from silent incorrect casts, but of course it doesn't, because C hates programmers.
- dllthomas 13y agoWell, typedef "defines a type", but (unfortunately) in a purely syntactic sense. If you're using typedef for something that's not wrapped in a struct or union, think hard about whether you mightn't want to also do that.