diff --git a/src/Compat.jl b/src/Compat.jl index f68b9c8ab98af..995b83d10ac2e 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -298,6 +298,19 @@ if VERSION < v"0.4.0-dev+656" include("nullable.jl") end +if VERSION < v"0.4.0-dev+3864" + function tryparse(T::Type{Float32}, s) + r = Array(T,1) + float32_isvalid(s, r) ? Nullable(r[1]) : Nullable{Float32}() + end + + function tryparse(T::Type{Float64}, s) + r = Array(T,1) + float64_isvalid(s, r) ? Nullable(r[1]) : Nullable{Float64}() + end + export tryparse +end + if VERSION < v"0.4.0-dev+3844" @eval module Libc const FILE = Base.CFILE diff --git a/test/runtests.jl b/test/runtests.jl index ba57a46072ccd..d106b287f59e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -192,6 +192,12 @@ end @test parse(Float64, "222") == 222.0 @test parse(Float32, "1.1") == convert(Float32, 1.1) @test parse(BigFloat, "1.125") == convert(BigFloat, 1.125) +@test isa(tryparse(Float32, "1.1"), Nullable) +@test get(tryparse(Float32, "1.1")) == 1.1f0 +@test isa(tryparse(Float32, "a"), Nullable{Float32}) +@test isa(tryparse(Float64, "1.1"), Nullable) +@test get(tryparse(Float64, "1.1")) == 1.1 +@test isa(tryparse(Float64, "a"), Nullable{Float64}) # Make sure exports from Libc and Libdl are defined for x in [:strftime,:systemsleep,:getpid,:FILE,:malloc,:MS_SYNC,:munmap,:flush_cstdio,:realloc,:strptime,:Libc,:errno,:msync,:TmStruct,:calloc,:MS_INVALIDATE,:MS_ASYNC,:time,:strerror,:gethostname,:free,:mmap]