2 ms·
Cool. Somewhat different but similar... In Julia, types with no fields are automatically singletons and an array size-zero immutable values takes no storage:
by StefanKarpinski 12y ago
Cool. Somewhat different but similar... In Julia, types with no fields are automatically singletons and an array size-zero immutable values takes no storage:
julia> sweet_nothings = Array(Nothing,typemax(Int))
9223372036854775807-element Array{Nothing,1}:
nothing
nothing
⋮
nothing
nothing
julia> sizeof(sweet_nothings)
0
This seems like a cute, useless trick but sometimes it's quite useful. For example, the Set{T} is implemented by wrapping a Dict{T,Nothing} and the array of values in the Dict object takes no storage and of course all access is optimized away.