top | item 34293405

(no title)

IChooseY0u | 3 years ago

What's the point of this? Just use an int.

typedef struct { int val; } meters_t;

discuss

order

6equj5|3 years ago

To me, the point of it is just to be more explicit and ease maintenance, like `typedef int meters_t`, but more extensible. Compare `int foo_len` to `meters_t foo_len`.

Explicitness: Consider the ambiguousness of `int foo_len` when you're swapping between meters and feet (but use `int` for both).

Ease maintenance: If you want to change the type you use to represent meters (say from `int` to `size_t`, since it might be wise to make it unsigned), compare going through the code and changing each meter-related instance of `int` vs. just changing the `meters_t` struct declaration.

More extensible: Compared to `typedef int meters_t`, using a struct is useful if you ever have to add to the struct. (Maybe a second numeric member representing the conversion to some other unit of length or something.)

For "meters", this doesn't really apply, but using a struct also prevents you from accidentally trying to do math with numeric types that you shouldn't do math with (like ID numbers or something): https://stackoverflow.com/a/18876104/9959012

Also, you probably shouldn't use `_t` at the end since that should be reserved by the C standard and by POSIX: https://stackoverflow.com/a/231807/9959012

hot_gril|3 years ago

Where I work, the C++ style guide explicitly says not to alias int types like this cause it becomes annoying for others to keep looking up what each thing really is, and probably YAGNI. I agree with that recommendation.

Any int variable storing meters is probably called "meters" already.

lionkor|3 years ago

to avoid implicit conversion - you can implicitly convert between meters and ft if its like this:

    typedef int feet;
    typedef int meters;
this sucks, because they are the same to the compiler. Making them structs makes them unique types, and you can no longer pass a feet value to a meters parameter, etc.

Its incredibly useful

BenFrantzDale|3 years ago

It’s to add minimal type safety. I use this technique in C++ (not with units) even though we have a strong units library. It’s particularly useful for keeping track of things that are scalar-like but get passed around a bunch. Passing around a “badness” in an optimization for example: just a `float` looses its meaning quickly and is hard to track down the comment saying what it means. with `/* Represents the badness of fit to be minimized during optimization… */ struct Badness { float badness; };` you can always find the struct and the documentation next to it. And you can have functions taking and returning them.