Skip to content

Commit

Permalink
Merge pull request JuliaLang#40386 from JuliaLang/fe/date-parse
Browse files Browse the repository at this point in the history
Add type asserts to (try)parse(::TimeType, ...) to help inference.
  • Loading branch information
quinnj committed Apr 13, 2021
2 parents 53603f6 + 8a14ef2 commit 2ba139c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions stdlib/Dates/src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function Base.parse(::Type{T}, str::AbstractString, df::DateFormat=default_forma
val = tryparsenext_internal(T, str, pos, len, df, true)
@assert val !== nothing
values, endpos = val
return T(values...)
return T(values...)::T
end

function Base.tryparse(::Type{T}, str::AbstractString, df::DateFormat=default_format(T)) where T<:TimeType
Expand All @@ -292,7 +292,7 @@ function Base.tryparse(::Type{T}, str::AbstractString, df::DateFormat=default_fo
values, endpos = res
if validargs(T, values...) === nothing
# TODO: validargs gets called twice, since it's called again in the T constructor
return T(values...)
return T(values...)::T
end
return nothing
end
Expand Down
10 changes: 10 additions & 0 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,14 @@ end
end
end

@testset "inference with dynamic dateformat string" begin
datetime = DateTime(2020, 4, 7)
f1() = DateTime("2020-04-07", "yyyy-mm-dd")
f2() = DateTime("2020-04-07", DateFormat("yyyy-mm-dd"))
f3() = parse(DateTime, "2020-04-07", DateFormat("yyyy-mm-dd"))
@test (@inferred f1()) == (@inferred f2()) == (@inferred f3()) == datetime
g() = tryparse(DateTime, "2020-04-07", DateFormat("yyyy-mm-dd"))
@test (@inferred Nothing g()) == datetime
end

end

0 comments on commit 2ba139c

Please sign in to comment.