Thanks @mbauman! With the previous example I had to do things like
Base.print_matrix(io::IO, mat::DirichletArray, args...) =
Base.print_matrix(io, mat.a, args...)
To stop Julia from complaining about method errors, but now things seem quite hunky-dory! Looking at the code in https://github.com/JuliaMath/Interpolations.jl/tree/master/src/extrapolation, I see that this is not how the Interpolations.jl
folk do things. However, I am happy with this current solution because it behaves the way that I want it to for my particular problem.
If anyone reading this is interested in a MWE, then this is what I have put together so far:
using Interpolations
immutable DirichletArray{T, N, S <: AbstractArray} <: AbstractArray{T, N}
a::S
oob_val::Tuple{T, T}
end
# And now you need an outer constructor that computes those parameters for you:
DirichletArray{T,N}(a::AbstractArray{T,N}, oob_val::Tuple{T,T}) = DirichletArray{T,N,typeof(a)}(a, oob_val)
Base.size(A::DirichletArray) = size(A.a)
Base.linearindexing{T<:DirichletArray}(::Type{T}) = Base.LinearFast()
function Base.getindex(A::DirichletArray, I...)
checkbounds(Bool, A.a, I...) && return A.a[I...]
I... < 1 ? A.oob_val[1] : A.oob_val[2]
end
a = [1.0,2.0,3.0,4.0]
a = interpolate!(a, BSpline(Linear()), OnGrid())
A = DirichletArray(a, (3.0,2.0))
A[1] # 1.0
A[3.5] # 3.5
A[0] # 3.0
A[5.1] # 2.0
Thanks again for all of the help everyone.