top | item 41033737

(no title)

proaralyst | 1 year ago

Void is different from this type though, as a variable of type void can't be occupied.

In ML and friends monostate is called unit (and gets used a lot because void returns aren't allowed by the languages). Some have empty types too, which can never be occupied. A function returning Empty can't return, for example, though there are other use cases

discuss

order

cvoss|1 year ago

You are equivocating on the word "void". Your statement that "a variable of type void can't be occupied" is true in functional languages where "void/Void" is often used as the name of a type that isn't inhabited (assuming the language is sound/normalizing/whatever).

But here we are talking about C++, where "void" is a pseudotype that is absolutely inhabited, in some conceptual sense. Any function that is declared to return void and which returns is returning a thing that conceptually inhabits void. In this sense, std::monostate indeed captures the same concept as void, but in a much better way, because it's properly a type, not a pseudotype.

Note: Java does the same thing, effectively, with "Void" which is inhabited by exactly one value: null.

proaralyst|1 year ago

I think it's not correct to say that void is a monotype in C++, because the compiler won't allow you to assign the result of a function marked void to a variable, and you cannot declare a variable of type void.

I'd accept that it's not the same as the empty type though, given that void* can be occupied and functions marked void can return. Probably someone with more type theory than me can name this properly

Koshkin|1 year ago

Incidentally, the classic C did not have 'void'; instead, it was assumed that any function would, by default, return 'int' in the form of some value stored in the accumulator, and so the "value" of 'void' would be effectively represented by random garbage. The 'void' that was introduced explicitly in a later version of C weakened the original meaning of the unknown value by allowing pointers to 'void' and thus not requiring that the value pointed to must be always thought of as meaningless (since you could cast a pointer to void to a pointer to something else).