Structuring a Type with Exception Messages

You could use the parameters package with something like this:

julia> using Parameters
       @with_kw struct NumMatrix{T}
         A::T
         @assert A isa AbstractMatrix "A must be a matrix"
         @assert eltype(A) <: Number "eltype(A) must be a number" 
       end
NumMatrix

julia> NumMatrix(zeros(2,2))
NumMatrix{Matrix{Float64}}
  A: Array{Float64}((2, 2)) [0.0 0.0; 0.0 0.0]


julia> NumMatrix([1,2])
ERROR: AssertionError: A must be a matrix

julia> NumMatrix([ 'a' 'b' ; 'c' 'd' ])
ERROR: AssertionError: eltype(A) must be a number

1 Like