How to use more complex type inheritance in Julia

I am trying to use the regular type Any in the following functions:

function f(arr::Array{Tuple{ASCIIString , Any},1})
arr[1]
end

It applies to

f([("a ",1), ("b","x")])

But in

f([("a",1)] )

This will not work. Some people would think that Int is actually Any, but obviously not.

How to work in the latter case? I am interested in a general solution, because this problem has appeared in many places in Julia, the above is just a simple example. Should I use all types of alliances in the tuple instead of Any?

The type parameters in Julia are unchanged, please refer to http://julia.readthedocs. org/en/latest/manual/types/#parametric-composite-types. To get the behavior you are after, you need to parameterize the function with type parameters:

function f{T <: Tuple{ASCIIString, Any}}(arr::Array{T,1})
arr[1]
end

I tried to use the regular type Any in the following functions:

function f(arr::Array{Tuple{ASCIIString, Any},1} )
arr[1]
end

It applies to

f([("a",1), ( "b","x")])

But in

f([("a",1)])

< p>This won’t work. Some people would think that Int is actually Any, but obviously not.

How to work in the latter case? I am interested in a general solution, because this problem has appeared in many places in Julia, the above is just a simple example. Should I use all types of alliances in the tuple instead of Any?

The type parameters in Julia are unchanged, please refer to http://julia.readthedocs.org/en/latest/manual/types/#parametric -composite-types. To get the behavior you are after, you need to parameterize the function with type parameters:

function f{T <: Tuple{ASCIIString , Any}}(arr::Array{T,1})
arr[1]
end

Leave a Comment

Your email address will not be published.