Skip to content
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

Add [] as an alternative to get #20

Merged
merged 1 commit into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# DataValues.jl v0.2.0 Release Notes
* Add [] as an alternative to get

# DataValues.jl v0.1.0 Release Notes
* Add configurable whitelist lifting

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ semantics for ``DataValue``s.
``DataValue`` arguments without the use of the dot broadcast syntax.
- The package provides a configurable whitelist approach to lifting (see
below).
- One can access or unpack the value within a ``DataValue`` either via the
``get(x)`` function, or use the ``x[]`` syntax.

Any help with this package would be greatly appreciated!

Expand Down
8 changes: 8 additions & 0 deletions src/DataValues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ end

get(x::DataValue) = isnull(x) ? throw(DataValueException()) : x.value

"""
getindex(x::DataValue)

Attempt to access the value of `x`. Throw a `DataValueException` if the
value is not present. Usually, this is written as `x[]`.
"""
Base.getindex(x::DataValue) = isnull(x) ? throw(DataValueException()) : x.value

get(x::DataValue{Union{}}) = throw(DataValueException())
get(x::DataValue{Union{}}, y) = y

Expand Down
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ for T in types
x3 = DataValue(one(T))

@test_throws DataValueException get(x1)
@test get(x2) === zero(T)
@test get(x3) === one(T)
@test_throws DataValueException x1[]
@test get(x2) === x2[] === zero(T)
@test get(x3) === x3[] === one(T)
end

@test_throws DataValueException get(DataValue())
Expand Down