Just started to experiment with implementing a proxy type, but I failed very early:
module Proxies
export AbstractProxy, Proxy
abstract type AbstractProxy end
struct Proxy{TInner} <: AbstractProxy
inner::TInner
Proxy{TInner}(inner) where TInner = new{TInner}(inner)
Proxy(inner) = begin
p = Proxy{typeof(inner)}(inner)
return p
end
end
inner(p) = p.inner
Base.convert(targettype::Type{<:T}, p::Proxy{TInner}) where {T, TInner} = begin
return convert(targettype, inner(p))
end
end # module
using Proxies
using Test
@testset "Proxy creation" begin
p = Proxy(42)
@test p isa Proxy
@test Proxies.inner(p) == 42
end
@testset "transparent proxing" begin
@test convert(Int, Proxy(42)) isa Int
@test convert(Int, Proxy(42.0)) isa Int
@test Proxy(42) == 42
@test_broken Proxy(42) + 42 == 84
end
Test Summary: | Pass Total
Proxy creation | 2 2
Test Summary: | Pass Total
transparent proxing | 2 2
ERROR: LoadError: MethodError: convert(::Type{Any}, ::Proxy{Int64}) is ambiguous. Candidates:
convert(::Type{Any}, x) in Base at essentials.jl:170
convert(targettype::Type{var"#s13"} where var"#s13"<:T, p::Proxy{TInner}) where {T, TInner} in Proxies at /home/krisztian/projects/Proxies.jl/src/Proxies.jl:19
Possible fix, define
convert(::Type{Any}, ::Proxy{TInner}) where TInner
Stacktrace:
[1] Base.RefValue{Any}(::Proxy{Int64}) at ./refvalue.jl:8
[2] Ref{Any}(::Proxy{Int64}) at ./refpointer.jl:96
[3] inferencebarrier(::Any) at ./essentials.jl:718
[4] show_default at ./show.jl:389 [inlined]
[5] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:384
[6] show_delim_array(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Tuple{DataType,Proxy{Int64}}, ::Char, ::Char, ::Char, ::Bool, ::Int64, ::Int64) at ./show.jl:776
[7] show_delim_array at ./show.jl:761 [inlined]
[8] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Tuple{DataType,Proxy{Int64}}) at ./show.jl:794
[9] _show_default(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:406
[10] show_default at ./show.jl:389 [inlined]
[11] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:384
[12] sprint(::Function, ::MethodError; context::Pair{Symbol,Bool}, sizehint::Int64) at ./strings/io.jl:103
[13] Test.Error(::Any, ::Any, ::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:162
[14] top-level scope at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1121
[15] top-level scope at /home/krisztian/projects/Proxies.jl/test/runtests.jl:11
[16] include(::String) at ./client.jl:457
[17] top-level scope at none:6
in expression starting at /home/krisztian/projects/Proxies.jl/test/runtests.jl:10
caused by [exception 1]
MethodError: convert(::Type{Any}, ::Proxy{Int64}) is ambiguous. Candidates:
convert(::Type{Any}, x) in Base at essentials.jl:170
convert(targettype::Type{var"#s13"} where var"#s13"<:T, p::Proxy{TInner}) where {T, TInner} in Proxies at /home/krisztian/projects/Proxies.jl/src/Proxies.jl:19
Possible fix, define
convert(::Type{Any}, ::Proxy{TInner}) where TInner
Stacktrace:
[1] Base.RefValue{Any}(::Proxy{Int64}) at ./refvalue.jl:8
[2] Ref{Any}(::Proxy{Int64}) at ./refpointer.jl:96
[3] inferencebarrier(::Any) at ./essentials.jl:718
[4] show_default at ./show.jl:389 [inlined]
[5] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:384
[6] show_delim_array(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Tuple{DataType,Proxy{Int64}}, ::Char, ::Char, ::Char, ::Bool, ::Int64, ::Int64) at ./show.jl:776
[7] show_delim_array at ./show.jl:761 [inlined]
[8] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Tuple{DataType,Proxy{Int64}}) at ./show.jl:794
[9] _show_default(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:406
[10] show_default at ./show.jl:389 [inlined]
[11] show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::Any) at ./show.jl:384
[12] sprint(::Function, ::MethodError; context::Pair{Symbol,Bool}, sizehint::Int64) at ./strings/io.jl:103
[13] Test.Error(::Any, ::Any, ::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:162
[14] do_test(::Test.ExecutionResult, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:518
[15] top-level scope at /home/krisztian/projects/Proxies.jl/test/runtests.jl:13
[16] top-level scope at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1115
[17] top-level scope at /home/krisztian/projects/Proxies.jl/test/runtests.jl:11
[18] include(::String) at ./client.jl:457
[19] top-level scope at none:6
ERROR: Package Proxies errored during testing
I would really appreciate any help on this!