Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Type changes based on declaration

$
0
0

@squirrel wrote:

Why aren’t F and V below of the same type? How can I write a function that will handle both of them (but not also any generic vector)?

using SparseArrays

F = Vector{SparseVector}()
push!(F, sparse([1, -1, 5, -5]))
push!(F, sparse([4, -4, -3, 3]))
push!(F, sparse([3, -3, 1, -4]))
push!(F, sparse([8, -8, 1, -1]))

V = [
    sparse([1, -1, 5, -5]),
    sparse([4, -4, -3, 3]),
    sparse([3, -3, 1, -4]),
    sparse([8, -8, 1, -1])
]

typeof(F) # Vector{SparseVector}
typeof(V) # Vector{SparseVector{Int64, Int64}}

So, for example, this function

function foo(V::Vector{SparseVector})
    println("My function 'tis of thee.")
end

only works on F, and this function

function foo(V::Vector{SparseVector{Int,Int}})
    println("Sweet Int of Int I see.")
end

only works on V. I’d like to write a function that will work on both.

Posts: 5

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images