It would be nice to have a scalar type for SignedArray perhaps so that just because we index it with a single Integer does not mean that we now get returned an Integer type and lose the associate information in the sign field.
So what I did was create a struct Signed like so:
struct Signed <: Number
data :: Number
sign :: String
function Signed(data :: Number, sign :: String)
if sign == "signed"
return new(data, sign)
elseif sign == "unsigned"
return new(abs(data), sign)
end
end
end
and then change
Base.getindex(sa::SignedArray, I::Integer) = sa.data[I] to Base.getindex(sa::SignedArray, I::Integer) = Signed(sa.data[I], sa.sign)
which kind of works i.e:
R = SignedArray([1,2,3,4], "unsigned")
q = R[2]
println(q)
s = R[1:3]
println(s)
gives
Signed(2, "unsigned")
[Signed(1, "unsigned"), Signed(2, "unsigned"), Signed(3, "unsigned")]
but breaks my setindex! function and I get the following error from running the following code
:
V = SignedArray(zeros(Int64, 10), "unsigned")
V[3:6] = R
>> MethodError: no method matching Int64(::Signed)
Is this a simple fix or have I opened a can of worms
?