File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,30 @@ module ParseUnparse
44
44
function Base. iterate (:: Optional , :: Any )
45
45
nothing
46
46
end
47
+ function convert_from_other_vector_eltype (:: Type{Optional{T}} , :: Type ) where {T}
48
+ T
49
+ end
50
+ function convert_from_other_vector_eltype (:: Type{Optional} , :: Type{T} ) where {T}
51
+ T
52
+ end
53
+ function convert_from_other_vector (:: Type{O} , vec:: AbstractVector ) where {O <: Optional }
54
+ if 1 < length (vec)
55
+ throw (ArgumentError (" got vector with more than one element, too long" ))
56
+ end
57
+ T = convert_from_other_vector_eltype (O, eltype (vec))
58
+ Op = Optional{T}
59
+ if isempty (vec)
60
+ Op ()
61
+ else
62
+ Op (only (vec))
63
+ end
64
+ end
65
+ function Base. convert (:: Type{Optional{T}} , vec:: AbstractVector ) where {T}
66
+ convert_from_other_vector (Optional{T}, vec)
67
+ end
68
+ function Base. convert (:: Type{Optional} , vec:: AbstractVector )
69
+ convert_from_other_vector (Optional, vec)
70
+ end
47
71
end
48
72
"""
49
73
ContextFreeGrammarUtil::Module
Original file line number Diff line number Diff line change @@ -19,4 +19,12 @@ using Test
19
19
@test [3 ] == collect (Float32, Optional (3 ))
20
20
@test_throws BoundsError Optional (3.0 )[2 ]
21
21
@test_throws BoundsError Optional {Int} ()[1 ]
22
+ @test (@inferred convert (Optional, Float32[])) isa Optional{Float32}
23
+ @test isempty (convert (Optional, Float32[]))
24
+ @test Optional (3 ) === convert (Optional, [3 ])
25
+ @test (@inferred convert (Optional{Float64}, Float32[])) isa Optional{Float64}
26
+ @test isempty (convert (Optional{Float64}, Float32[]))
27
+ @test Optional (3.0 ) === convert (Optional{Float64}, [3 ])
28
+ @test_throws ArgumentError convert (Optional, [3 , 7 ])
29
+ @test_throws ArgumentError convert (Optional{Float64}, [3 , 7 ])
22
30
end
You can’t perform that action at this time.
0 commit comments