Optional variable declarations

I would code the initial example repeated below

var x::Vector
var y::(Vector,Matrix)

x=eig(A)  # Computes only eigenvalues
y=eig(A) # Computes both eigenvalues and eigenvectors

as this:

function eig!(A, x :: Vector)
    ...
end
function eig!(A, x :: Vector, z :: Matrix)
    ...
end
x = Vector(undef, size1)
z = Matrix(undef, size1, size2)

eig!(A, x)  # Computes only eigenvalues
eig!(A, x, z) # Computes both eigenvalues and eigenvectors

Not sure why those output types would be necessary unless it is for returning immutable structs, and in such case you can either dispatch on RefValue wrappers or just change the function name as it was said.

1 Like

Argh. Now that song is in my head.