If you have a signature
function commonprefix(str_arr::Array{AbstractString,1})
It means that it should only work on Arrays with element type
AbstractString. If you call it with Array{UTF8String,1}, you can't push! a
reference to a ASCIIString onto the array (without conversion), so the
method does not apply. This is an arbitrary choice that the designers of
Julia made that most users has questioned the decision at some point. The
technical terms are invariance and contravariance.
Ivar
mandag 16. mars 2015 15.24.13 UTC+1 skrev Sisyphuss følgende:
>
> I have just made the same mistake this morning.
>
>
>
> On Monday, March 16, 2015 at 1:05:32 PM UTC+1, Simon Danisch wrote:
>>
>> It should be commonprefix{S <: AbstractString}(str_arr::Vector{S})
>> (Vector -> typealias Vector{T} Array{T, 1})
>> What you told julia with Vector{AbstractString} is that the function only
>> takes Vectors with the element type of AbstractString.
>> Though, ["asdasd", "adasd"] has the concrete element type of ASCIIString
>>
>> Am Montag, 16. März 2015 12:50:19 UTC+1 schrieb Julia User:
>>>
>>> just some simple beginners question:
>>>
>>>
>>> *1. Question:* Why does *Array{AbstractString,1}*: not work?
>>>
>>> ERROR: LoadError: MethodError: `commonprefix` has no method matching
>>> commonprefix(::Array{ASCIIString,1})
>>>
>>>
>>> function commonprefix(str_arr::Array{AbstractString,1})
>>> min_idx = indmin(str_arr)
>>> common_prefix = str_arr[min_idx]
>>> common_length = length(common_prefix)
>>> # Other code skipped
>>> return common_length
>>> end
>>>
>>> common_endidx = commonprefix(["/home/Downloads/test",
>>> "/home/Downloads/othertest"])
>>> println("AbstractString: $common_endidx")
>>>
>>>
>>>
>>>
>>>
>>>
>>> *2. Question:* is there an easy way to avoid rewriting most of the
>>> functions code
>>>
>>>
>>>
>>> function commonprefix(str_arr::Array{ASCIIString,1})
>>> min_idx = indmin(str_arr)
>>> common_prefix = str_arr[min_idx]
>>> common_length = length(common_prefix)
>>> # Other code skipped
>>> return common_length
>>> end
>>>
>>> function commonprefix(str_arr::Array{UTF8String,1})
>>> min_idx = indmin(str_arr)
>>> common_prefix = str_arr[min_idx]
>>> common_length = length(common_prefix)
>>> # Other code skipped
>>> return common_length
>>> end
>>>
>>>
>>> # ASCIIString
>>> common_endidx = commonprefix(["/home/Downloads/test",
>>> "/home/Downloads/othertest"])
>>> println("ASCIIString: $common_endidx")
>>> # UTF8String
>>> common_endidx = commonprefix(["/home/Downloads/átomos",
>>> "/home/Downloads/otherátomos"])
>>> println("UTF8String: $common_endidx")
>>>
>>>
>>>
>>> Thanks
>>>
>>>
>>>