# Writing a new type for a specific kind of array (constructor error)

**URL:** <https://discourse.julialang.org/t/writing-a-new-type-for-a-specific-kind-of-array-constructor-error/62900>\
**Category:** New to Julia\
**Created:** [June 14, 2021, 5:43pm UTC](https://discourse.julialang.org/t/writing-a-new-type-for-a-specific-kind-of-array-constructor-error/62900 "2021-06-14T17:43:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 14, 2021, 6:16pm UTC](https://discourse.julialang.org/t/writing-a-new-type-for-a-specific-kind-of-array-constructor-error/62900/2 "2021-06-14T18:16:09Z")

</div>

Welcome @gpownall !

Here you go:

```julia
struct ProbMatrix{T<:Real} <: AbstractArray{T,2}
        data
        function ProbMatrix(data::Array{T,2}) where T<:Real
                if ndims(data) != 2
                        error("Probability matrix needs to be two-dimensional")
                end
                if size(data,1) != size(data,2)
                        error("Probability matrix needs to be square.")
                end
                for col_num in 1:size(data,1)
                        if sum(data[:,col_num]) != 1
                                error("Each column of a probability matrix must sum to one.")
                        end
                end
                new{T}(data)
        end
end

Base.size(m::ProbMatrix{T}) where T = size(m.data)

Mat = [0.8 0.05 ; 0.2 0.95]

println(inv(Mat))

A = ProbMatrix(Mat);

```

As your struct is subtyped `AbstractArray{T,2}` , `size` needs to be defined for the new type.

There is more, you have to define, e.g. `getindex` . You get the error if you omit the `;` in the last line:

```julia
julia> A = ProbMatrix(Mat)
2×2 ProbMatrix{Float64}:
Error showing value of type ProbMatrix{Float64}:
ERROR: getindex not defined for ProbMatrix{Float64}

```

We had another similar thread, where you can find a few details about what was wrong in general: [Inner constructor – checking for equal lengths](https://discourse.julialang.org/t/inner-constructor-checking-for-equal-lengths/62413)

---

_[View the full topic](https://discourse.julialang.org/t/writing-a-new-type-for-a-specific-kind-of-array-constructor-error/62900)._
