From 31ec9da58adf695ea39c38b282337086e5fa269c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 23 Aug 2017 16:10:32 -0500 Subject: [PATCH] Add method for get(f::Function, ::EnvHash, key) --- base/env.jl | 1 + test/env.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/base/env.jl b/base/env.jl index c6010c193cc6d..582f0d823436c 100644 --- a/base/env.jl +++ b/base/env.jl @@ -77,6 +77,7 @@ similar(::EnvHash) = Dict{String,String}() getindex(::EnvHash, k::AbstractString) = access_env(k->throw(KeyError(k)), k) get(::EnvHash, k::AbstractString, def) = access_env(k->def, k) +get(f::Callable, ::EnvHash, k::AbstractString) = access_env(k->f(), k) in(k::AbstractString, ::KeyIterator{EnvHash}) = _hasenv(k) pop!(::EnvHash, k::AbstractString) = (v = ENV[k]; _unsetenv(k); v) pop!(::EnvHash, k::AbstractString, def) = haskey(ENV,k) ? pop!(ENV,k) : def diff --git a/test/env.jl b/test/env.jl index 10c75d4da66b8..2a52d3b415d4b 100644 --- a/test/env.jl +++ b/test/env.jl @@ -30,6 +30,7 @@ key = randstring(25) @test !haskey(ENV,key) @test_throws KeyError ENV[key] @test get(ENV,key,"default") == "default" +@test get(() -> "default", ENV, key) == "default" # Test for #17956 @test length(ENV) > 1