From dc03c768c71e455ac07140c4c7e3b408bfda2ff5 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Sun, 2 Oct 2016 16:43:05 -0400 Subject: [PATCH 1/6] Allow x[] for get(x::Nullable) --- base/nullable.jl | 19 ++++++++++++++++--- doc/stdlib/base.rst | 10 ++++++++-- test/nullable.jl | 6 ++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/base/nullable.jl b/base/nullable.jl index dfcec4f68dd3e..e3e9bd62c1975 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -46,10 +46,10 @@ function show{T}(io::IO, x::Nullable{T}) end """ - get(x::Nullable[, y]) + get(x::Nullable, y) Attempt to access the value of `x`. Returns the value if it is present; -otherwise, returns `y` if provided, or throws a `NullException` if not. +otherwise, returns `y` if provided. """ @inline function get{S,T}(x::Nullable{S}, y::T) if isbits(S) @@ -59,7 +59,20 @@ otherwise, returns `y` if provided, or throws a `NullException` if not. end end -get(x::Nullable) = isnull(x) ? throw(NullException()) : x.value +""" + getindex(x::Nullable) + +Attempt to access the value of `x`. Throw a `NullException` if the value is not +present. Usually, this is written as `x[]`. +""" +getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value + +""" + get(x::Nullable) + +Alias for `getindex(x::Nullable)`. +""" +get(x::Nullable) = x[] """ unsafe_get(x) diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 435c662775994..d463a3870cc73 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -822,11 +822,17 @@ Nullables Wrap value ``x`` in an object of type ``Nullable``\ , which indicates whether a value is present. ``Nullable(x)`` yields a non-empty wrapper, and ``Nullable{T}()`` yields an empty instance of a wrapper that might contain a value of type ``T``\ . -.. function:: get(x::Nullable[, y]) +.. function:: get(x::Nullable, y) .. Docstring generated from Julia source - Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y`` if provided, or throws a ``NullException`` if not. + Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y`` if provided. + +.. function:: getindex(x::Nullable) + + .. Docstring generated from Julia source + + Attempt to access the value of ``x``\ . Throw a ``NullException`` if the value is not present. Usually, this is written as ``x[]``\ . .. function:: isnull(x) diff --git a/test/nullable.jl b/test/nullable.jl index 819cd4a5871ce..cb1f83c7ff37f 100644 --- a/test/nullable.jl +++ b/test/nullable.jl @@ -140,11 +140,13 @@ for T in types x3 = Nullable(one(T)) @test_throws NullException get(x1) - @test get(x2) === zero(T) - @test get(x3) === one(T) + @test_throws NullException x1[] + @test get(x2) === x2[] === zero(T) + @test get(x3) === x3[] === one(T) end @test_throws NullException get(Nullable()) +@test_throws NullException Nullable()[] # get{S, T}(x::Nullable{S}, y::T) for T in types From cb73726f4a24b070427c7b25fb9a522ddba7aca1 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Sun, 2 Oct 2016 17:02:04 -0400 Subject: [PATCH 2/6] Remove "if provided" --- base/nullable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/nullable.jl b/base/nullable.jl index e3e9bd62c1975..c58358b7fd3bc 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -49,7 +49,7 @@ end get(x::Nullable, y) Attempt to access the value of `x`. Returns the value if it is present; -otherwise, returns `y` if provided. +otherwise, returns `y`. """ @inline function get{S,T}(x::Nullable{S}, y::T) if isbits(S) From 9a174a3102e9ddd1995d6c175a653f928a04f5cd Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Sun, 2 Oct 2016 17:12:34 -0400 Subject: [PATCH 3/6] Give direct information in docstring --- base/nullable.jl | 3 ++- doc/stdlib/base.rst | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/base/nullable.jl b/base/nullable.jl index c58358b7fd3bc..2a322cd6d4876 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -70,7 +70,8 @@ getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value """ get(x::Nullable) -Alias for `getindex(x::Nullable)`. +Attempt to access the value of `x`. Throw a `NullException` if the value is not +present. """ get(x::Nullable) = x[] diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index d463a3870cc73..c56991bb9ebc5 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -826,7 +826,7 @@ Nullables .. Docstring generated from Julia source - Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y`` if provided. + Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y``\ . .. function:: getindex(x::Nullable) From c550628775946b10da2d8fd86cddf1ef3681a937 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 11 Oct 2016 20:04:39 -0400 Subject: [PATCH 4/6] Add equivalency note --- base/nullable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/nullable.jl b/base/nullable.jl index 2a322cd6d4876..245a412693b53 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -71,7 +71,7 @@ getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value get(x::Nullable) Attempt to access the value of `x`. Throw a `NullException` if the value is not -present. +present. Equivalent to `getindex(x::Nullable)`. """ get(x::Nullable) = x[] From 88d2e0fe2d031dc5e48806bfe3cc29cab0750439 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 11 Oct 2016 20:05:40 -0400 Subject: [PATCH 5/6] Note syntax sugar --- base/nullable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/nullable.jl b/base/nullable.jl index 245a412693b53..f8aaa7c295390 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -71,7 +71,7 @@ getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value get(x::Nullable) Attempt to access the value of `x`. Throw a `NullException` if the value is not -present. Equivalent to `getindex(x::Nullable)`. +present. Equivalent to `getindex(x::Nullable)`, which can be spelt `x[]`. """ get(x::Nullable) = x[] From 47ea4c91791612dc022ec24962345a49842870f0 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 11 Oct 2016 20:32:35 -0400 Subject: [PATCH 6/6] Improve clarity of docstring --- base/nullable.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/nullable.jl b/base/nullable.jl index f8aaa7c295390..cece014371893 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -71,7 +71,8 @@ getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value get(x::Nullable) Attempt to access the value of `x`. Throw a `NullException` if the value is not -present. Equivalent to `getindex(x::Nullable)`, which can be spelt `x[]`. +present. Equivalent to `getindex(x::Nullable)`, which can alternatively be +written as `x[]`. """ get(x::Nullable) = x[]