import Base.+

if VERSION != v"0.6.1" && VERSION != v"0.6"
    error("Expected julia version to be 0.6 or 0.6.1. I'm outta here.")
end

struct SingleDiag{T<:Real} <: AbstractArray{T, 2}
    diag::Array{T, 1}
end

# Size
Base.size(sd::SingleDiag) = (length(sd.diag), length(sd.diag))

# Indexing (bracket operator)
function Base.getindex(sd::SingleDiag, indices::Vararg{Int, 2})
    (i, j) = indices
    return i != j ? 0 : sd.diag[i]
end

# Indexed Assignment (bracket operator assignment)
function Base.setindex!{T}(sd::SingleDiag{T}, val::T, indices::Vararg{Int, 2})
    (i, j) = indices
    if i != j
        error("Can't assign outside diagonal")
    end

    sd.diag[i] = val

    return sd
end

# Addition
function +(x::SingleDiag, y::SingleDiag)
    if size(x) != size(y)
        error("Dimension mismatch")
    end

    SingleDiag(x.diag + y.diag)
end

s = SingleDiag([1, 2, 3, 4, 5])  # SingleDiag{Int64}

@show size(s)    # (5, 5)

@show s[2, 2]    # 2
@show s[2, 1]    # 0

s[2, 2] = 10
@show s          # [1 0 0 0 0; 0 10 0 0 0; 0 0 3 0 0; 0 0 0 4 0; 0 0 0 0 5]

@show s + s      # SingleDiag{Int64}

@show s * 2      # Array{Int64,2}

@show s * eye(5) # Array{Flota64,2}

a = reshape(1:25, (5, 5))  # Array{Int64, 2}

@show x = s + a  # Array{Int64, 2}
@show typeof(x)

# You can compare AbstractArrays to other AbstractArrays
@show (s + s) == (s * 2)