Skip to content

Commit 361741b

Browse files
authored
Merge pull request #135 from aminya/tricks
using Tricks.static_hasmethod
2 parents 1c8d276 + 12cee12 commit 361741b

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
88
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
99
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1010
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
11+
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
1112
TypeTransform = "0fb2cfb0-ef68-4639-af2f-cb92dac991ae"
1213

1314
[compat]
@@ -16,6 +17,7 @@ PrettyTables = "0.8, 0.9"
1617
Reexport = "^0.2.0"
1718
Tables = "0.2, 1.0"
1819
TypeTransform = "0.1.3"
20+
Tricks = "0.1.3"
1921
julia = "1.2"
2022

2123
[extras]

benchmark/benchmark_result.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Benchmark Result
2+
3+
v 0.10.3
4+
Outside Atom
5+
8.700 μs (100 allocations: 3.61 KiB)
6+
3.957 μs (66 allocations: 3.17 KiB)
7+
201.799 μs (401 allocations: 16.27 KiB)
8+
29
v 0.10.2
310
Outside Atom
411
8.700 μs (100 allocations: 3.61 KiB)

src/xmlutils/nodeparse.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# TODO use traits for hasmethod checks
1+
using Tricks: compat_hasmethod
2+
# TODO use traits for compat_hasmethod checks
23

34
# Scalar parsers
45

@@ -17,9 +18,9 @@ function nodeparse(type::Type, content::String)
1718
# https://julialang.slack.com/archives/C6A044SQH/p1578442480438100
1819
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
1920
return type(content)
20-
elseif hasmethod(convert, Tuple{String, type})
21+
elseif compat_hasmethod(convert, Tuple{String, type})
2122
return convert(type, content)
22-
elseif hasmethod(parse, Tuple{type, String})
23+
elseif compat_hasmethod(parse, Tuple{type, String})
2324
return parse(type, content)
2425
else
2526
error("Could not parse a String as type $type")
@@ -32,9 +33,9 @@ function nodeparse(type::Type, elm::Node)
3233
content = elm.content
3334
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
3435
return type(content)
35-
elseif hasmethod(convert, Tuple{String, type})
36+
elseif compat_hasmethod(convert, Tuple{String, type})
3637
return convert(type, content)
37-
elseif hasmethod(parse, Tuple{type, String})
38+
elseif compat_hasmethod(parse, Tuple{type, String})
3839
return parse(type, content)
3940
else
4041
# should be the last to avoid invoking generic methods
@@ -58,17 +59,18 @@ end
5859
function nodeparse(::Type{T}, contents::Vector{String}) where {T}
5960
elms_typed = Vector{T}(undef, length(contents))
6061
i = 1
62+
# TODO https://github.com/oxinabox/Tricks.jl/issues/2#issuecomment-630450764
6163
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
6264
for content in contents
6365
elms_typed[i] = type(content)
6466
i+=1
6567
end
66-
elseif hasmethod(convert, Tuple{String, type})
68+
elseif compat_hasmethod(convert, Tuple{String, type})
6769
for content in contents
6870
elms_typed[i] = convert(type, content)
6971
i+=1
7072
end
71-
elseif hasmethod(parse, Tuple{type, String})
73+
elseif compat_hasmethod(parse, Tuple{type, String})
7274
for content in contents
7375
elms_typed[i] = parse(type, content)
7476
i+=1
@@ -82,17 +84,18 @@ end
8284
function nodeparse(type::Type{T}, elms::Vector{Node}) where {T}
8385
elms_typed = Vector{T}(undef, length(elms))
8486
i = 1
87+
# TODO https://github.com/oxinabox/Tricks.jl/issues/2#issuecomment-630450764
8588
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
8689
for elm in elms
8790
elms_typed[i] = type(elm.content)
8891
i+=1
8992
end
90-
elseif hasmethod(convert, Tuple{String, type})
93+
elseif compat_hasmethod(convert, Tuple{String, type})
9194
for elm in elms
9295
elms_typed[i] = convert(type, elm.content)
9396
i+=1
9497
end
95-
elseif hasmethod(parse, Tuple{type, String})
98+
elseif compat_hasmethod(parse, Tuple{type, String})
9699
for elm in elms
97100
elms_typed[i] = parse(type, elm.content)
98101
i+=1

0 commit comments

Comments
 (0)