Hacker News new | ask | show | jobs
by gpderetta 703 days ago
> C and C++ don't have a type spindle, where void would be at the bottom. Only C++ has the concept of subtype, only in the class system, and the C++ class system doesn't have a bottom type; there is no bottom class that is a base for all the others.

A bottom type is not the base of all other types.

> void is not a proper type; it's just a hack shoehorned into a convenient spot in the type system.

It is a type, but it is not Regular and it is incomplete. 'return x;' is invalid in a void-returning function because it doesn't type check. 'return void()' or 'return (void)0;' or 'return void_returning_function();' are all valid because they type check.

Making void regular has been proposed multiple times [1]. It is a relatively simple extension but nobody that cares has the time to carry it through standardization.

[1] https://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0146r1...

1 comments

In C, it is not like that. From the 2023 draft:

6.8.7.5 The return statement

Constraints

1 A return statement with an expression shall not appear in a function whose return type is void.

It's a constraint violation. It doesn't matter what the type of the expression is.

It looks as if C++ made a small improvement here.

Yes, the bottom type is at the bottom of the type derivation hierarchy. That's why the word bottom is there; that's what it's at the bottom of. It's also why it can't have any instances. Since every other type is a supertype, then if the bottom type contained some value V, that value would be imposed into every other type! V would be a valid String, Widget, Integer, Stream, Array ... what have you.

To clarify, it could be that the bottom type is not the base of all types, if the language has a split between some types which participate in that sort of thing and others that don't (e.g. class versus basic types or whatever).

But void is not the base of anything in C and C++.

You could argue that void is in some category of types where it is at the bottom; but no other types are in that category.

There is another problem: a bottom type should be the subtype of all types in that category. That includes being its own subtype. There we have a problem: C and C++ void is not a subtype of void in any sense.