Skip to content

Commit 3398c7e

Browse files
authored
Merge pull request #18736 from ninjin/nin/pairconv
RFC: Pair to Pair conversions
2 parents b728145 + 97a46c9 commit 3398c7e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/operators.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,11 @@ reverse{A,B}(p::Pair{A,B}) = Pair{B,A}(p.second, p.first)
964964
endof(p::Pair) = 2
965965
length(p::Pair) = 2
966966

967+
convert{A,B}(::Type{Pair{A,B}}, x::Pair{A,B}) = x
968+
function convert{A,B}(::Type{Pair{A,B}}, x::Pair)
969+
convert(A, x[1]) => convert(B, x[2])
970+
end
971+
967972
# some operators not defined yet
968973
global //, >:, <|, hcat, hvcat, , ×, , , , , , , , , , ,
969974

test/operators.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ B = [true true false]
2121
@test reverse(Pair(1,2)) == Pair(2,1)
2222
@test reverse(Pair("13","24")) == Pair("24","13")
2323
@test typeof(reverse(Pair{String,Int64}("a",1))) == Pair{Int64,String}
24+
@test convert(Pair{Float64,Float64}, 17 => 4711) === (17.0 => 4711.0)
25+
@test convert(Pair{Int,Float64}, 17 => 4711) === (17 => 4711.0)
26+
@test convert(Pair{Float64,Int}, 17 => 4711) === (17.0 => 4711)
2427

2528
p = 1=>:foo
2629
@test first(p) == 1

0 commit comments

Comments
 (0)