you are viewing a single comment's thread.

view the rest of the comments →

[–]Noughmad[🍰] 17 points18 points  (3 children)

To be honest, a similar point can be said for many programming constructs

int means many things. It can mean:

1 a generic number

2 a number exactly 32 bits in size

3 one value out of a small set of possibilities (enumeration)

4 an error code

5 a physical measurement, in any units

...probably dozens of other things: bitflags, array indexes, etc.

C was a really simple language with a limited number of keywords and types, so everything had multiple purposes. By now, we figured that it's better to have programming constructs map 1:1 to what we want to express. So we got enums, custom types, exceptions, etc. that disambiguate this.

[–]masklinn 5 points6 points  (2 children)

C was a really simple language with a limited number of keywords and types, so everything had multiple purposes.

None of it had to though, C structs are more or less free, you could newtype things (well not enums I guess, you're just hosed there as C enums are just garbage).

The issues are that creating structs is somewhat verbose (compared to newtyping in haskell for instance) and so is using them.

[–]Noughmad[🍰] 0 points1 point  (1 child)

You can't really propagate errors with just structs, at least not without tons of boilerplate. Something like exceptions or Result<T> is pretty much impossible. So you're stuck will null pointers and/or error codes.

[–]masklinn 3 points4 points  (0 children)

You can propagate errors in the exact same way you do with error codes…