bramtayl
bramtayl

Reputation: 4024

Subtypes of composite types

I'm trying to make a vectorized version of ismatch.

Base.ismatch
function ismatch(vector::Vector, regex::Regex)
  [ismatch(regex, string) for string in vector]
end

This works, but this

Base.ismatch
function ismatch(vector::Vector{String}, regex::Regex)
  [ismatch(regex, string) for string in vector]
end

doesn't because Vector{ASCIIString} <: Vector{String} is false.

Is there any way to get around this?

Upvotes: 2

Views: 167

Answers (1)

reschu
reschu

Reputation: 1105

The reason for your results is julias invariant typing system. This means, although ASCIIString <: String is true, Vector{ASCIIString} <: Vector{String} is false.

To get around, use parametric types:

import Base.ismatch
function ismatch{T<:String}(vector::Vector{T}, regex::Regex)
   [ismatch(regex,string), for string in vector]
end

Upvotes: 7

Related Questions