-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Empty(T) | ||
Create a proxy of an empty container of type `T`. | ||
This is a simple API for solving problems such as: | ||
* There is no consistent way to create an empty container given its type. | ||
* There is no consistent way to know that nothing was appended into the | ||
container in type-domain. | ||
Internally, this function simply works by creating a singleton container | ||
(a container with one element) using [`singletonof`](@ref) when the first | ||
element is [`push!!`](@ref)'ed. | ||
# Examples | ||
```jldoctest | ||
julia> using BangBang | ||
julia> push!!(Empty(Vector), 1) | ||
1-element Array{Int64,1}: | ||
1 | ||
julia> append!!(Empty(Dict), (:a=>1, :b=>2)) | ||
Dict{Symbol,Int64} with 2 entries: | ||
:a => 1 | ||
:b => 2 | ||
julia> using DataFrames: DataFrame | ||
julia> @assert push!!(Empty(DataFrame), (a=1, b=2)) == DataFrame(a=[1], b=[2]) | ||
julia> using StructArrays: StructVector | ||
julia> @assert push!!(Empty(StructVector), (a=1, b=2)) == StructVector(a=[1], b=[2]) | ||
julia> using TypedTables: Table | ||
julia> @assert push!!(Empty(Table), (a=1, b=2)) == Table(a=[1], b=[2]) | ||
``` | ||
""" | ||
struct Empty{T} end | ||
Empty(T::Type) = Empty{T}() | ||
|
||
push(::Empty{T}, x) where T = singletonof(T, x) |