mardi 14 novembre 2017

How do I check if a given `Method` object accepts a given `Tuple{DataType}` in Julia 0.6?

The goal is to define a function that takes a method, and a tuple of types, and returns true those types are a valid input for that method. Not function, method

accepts(meth::Method, types::Tuple)::Bool

Here is a testset for this

using Base.Test

concrete = first(methods(length, (String,)))
#length(s::String) in Base at strings/string.jl:162

abstract_ = last(collect(methods(length,(AbstractArray,))))
#length(t::AbstractArray) in Base at abstractarray.jl:131

triangular = methods(length, (StepRange,))
# length(r::StepRange{T,S} where S) where T<:Union{Int64, UInt64} in Base at range.jl:381

a = "hello"
a_t = (typeof(a),) #(String,)
@test accepts(concrete, a_t)
@test !accepts(abstract_, a_t)
@test !accepts(triangular, a_t)

b = 1.5:10
b_t = (typeof(b),) #(StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}})
@test !accepts(concrete, b_t)
@test accepts(abstract_, b_t)
@test !accepts(triangular, b_t)

c_t = (StepRange{Float64, Float64},) # Nothing real has this type as it can't be constructed
@test !accepts(concrete, c_t)
@test accepts(abstract_, c_t)
@test !accepts(triangular, c_t)

d = 1:10
d_t = (typeof(d),) #(StepRange{Int64,Int64}
@test !accepts(concrete, d_t)
@test accepts(abstract_, d_t)
@test accepts(triangular, d_t)

Since julia don't have much of a public reflection API (as I have griped before), I am fine with code that is going to break potentially every patch version

The goal of this question is to help me understand what I am going to need to do to update this part of InterfaceTesting.jl from 0.5 to 0.6

The thing that makes my old code no longer work is changes that were introduced to allow for triangular dispatch. So the triangular tests are the hard ones to pass.





Aucun commentaire:

Enregistrer un commentaire