-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Full void
type, including support for use in generics
#3194
Comments
It seems you want a singleton type (also called unit type). In Dart, A singleton type, on the other hand, could be introduced without many hassles, AFAIK. I don't know however, how it would fit in the Dart's current type system, as Dart already has a singleton type, which is IMO we already have a lot of confusion with top types (we have (Diverging a little, I would rather go in the direction of dropping I am not sure how the compiler treats |
Thanks for the answer. I wasn't aware of the Might make a follow up issue on arrays with comptime size and monomorphization when I get to implementing my B-Trees :) |
@appgurueu, as others have already mentioned, the meaning of the type You might want to take a look at the section '20.9 Type Void' in the language specification. (Note that the updates to include null safety are under review, so the link I just mentioned refers to the language specification that specifies the language before null safety. However, that doesn't make much of a difference in this particular section.) The main point is that an expression of type The returned value is a perfectly valid object. It is null in most cases, but it could be anything. The point is that the type The potential ability to detect that a particular type carries no information and generate special-cased code that avoids allocating any memory for any such non-informative entity is basically concerned with monomorphization, cf. dart-lang/sdk#52722. |
Proper "void" (zero-memory) types (the empty struct in Go; the empty tuple, also called "unit" in Rust) are extremely useful when you want to express a simpler data structure in terms of a more powerful one with zero overhead.
The prime example of this is implementing
Set
s in terms ofMap
s: ASet<T>
is effectively just aMap<T, void>
. You usually want your compiler to optimize the void type out completely (e.g. through monomorphization), such that you waste no memory and time on it.Without a proper void type, you either need to waste time and space for unnecessary values, such as using
bool
instead ofvoid
and setting the values to a constanttrue
/false
, or you need to duplicate your code and remove all instances where the values are being used, effectively performing the monomorphization "by hand".Proper metaprogramming (such as Zig's powerful
comptime
) poses a viable (though generally more cumbersome) alternative to a void type. This seems to be in the works?See also dart-lang/sdk#2231. To answer the questions posed there:
void
variables and arguments should only have one value:void
- thus the argument would just be a constantvoid
The list would be a list of contant
void
s. Sincevoid
s take no memory, theList
would need no memory except for storing its length. Getting elements would return constantvoid
s, adding elements would only modify the length. Effectively the list would pretty much degrade to an awkward counter. This isn't terribly useful; programmers will always be able to use features for nonsense however.And it does come in handy if you're implementing B-Trees, where you might want to maintain a list of values for a B-Tree based map: To implement a set in terms of this map, you'll have to set
V = void
, thus this list of values will become a list ofvoid
s. Ideally, to make this list ofvoid
s take zero memory, Dart would have to provide an array type where the size is known at compile time, like Go's arrays.The text was updated successfully, but these errors were encountered: