You need to define Base.show for this type.
julia> struct FiveToTwenty <: Unsigned
val::UInt8
function FiveToTwenty(val)
@boundscheck val >= 5 && val <= 20
new(UInt8(val))
end
end
julia> FiveToTwenty(14)
Error showing value of type FiveToTwenty:
ERROR: MethodError: no method matching leading_zeros(::FiveToTwenty)
Closest candidates are:
leading_zeros(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:238
Stacktrace:
[1] hex(::FiveToTwenty, ::Int64, ::Bool) at ./intfuncs.jl:543
[2] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::MIME{Symbol("text/plain")}, ::FiveToTwenty) at ./REPL.jl:122
[3] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::FiveToTwenty) at ./REPL.jl:125
[4] display(::FiveToTwenty) at ./multimedia.jl:218
[5] eval(::Module, ::Any) at ./boot.jl:235
[6] print_response(::Base.Terminals.TTYTerminal, ::Any, ::Void, ::Bool, ::Bool, ::Void) at ./REPL.jl:144
[7] print_response(::Base.REPL.LineEditREPL, ::Any, ::Void, ::Bool, ::Bool) at ./REPL.jl:129
[8] (::Base.REPL.#do_respond#16{Bool,Base.REPL.##26#36{Base.REPL.LineEditREPL,Base.REPL.REPLHistoryProvider},Base.REPL.LineEditREPL,Base.LineEdit.Prompt})(::Base.LineEdit.MIState, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Bool) at ./REPL.jl:646
julia> x = ans;
julia> Base.show(io::IO, v::FiveToTwenty) = print(io, v.val)
julia> x
14
The hint was that the error said âError showingâŚâ
You created the object just fine, but the error came in displaying the actual value. This is why we could still assign the value (without error), as long as we followed the operation with a semicolon.
Then, defining a show method let you display it.
In Julia, if you want a custom type with certain properties, you can just define it on your own.
If you donât like always having the error checking, you can
julia> struct FiveToTwenty2 <: Unsigned
val::UInt8
@inline function FiveToTwenty2(val)
@boundscheck begin @assert val >= 5 && val <= 20 end
new(UInt8(val))
end
end
julia> Base.show(io::IO, v::FiveToTwenty2) = print(io, v.val)
julia> f(x) = (@inbounds x = FiveToTwenty2(x); x)
f (generic function with 1 method)
julia> FiveToTwenty2(21)
ERROR: AssertionError: val >= 5 && val <= 20
Stacktrace:
[1] FiveToTwenty2(::Int64) at ./REPL[52]:4
julia> f(11)
11
julia> f(21)
21
Thus, once youâve confirmed your code never violates the bounds, you can add the @inbounds
and turn bounds checking off.
Although if someone else put a lot of time into a solution, of course itâs easier to take advantage of their work. Peopleâve asked a few times what the best way to actually discover packages is thoughâŚ
Anyway, if youâre facing constrained optimization problems with straightforward constraints (eg, 1 < y < infinity), a straightforward approach is as in:
Where you transform the variables from the constrained to an unconstrained version.
Eg, -infinity < log(y-1) < infinity. The above package handles transforms like this, as well as providing everything else, like Jacobians and the inverse transforms.
For the third case,
julia> using StaticArrays
julia> FiveVector1{T} = MVector{5,T}
StaticArrays.MArray{Tuple{5},T,1,5} where T
julia> FiveVector2{T} = SizedArray{Tuple{5},T}
StaticArrays.SizedArray{Tuple{5},T,N,M} where M where N where T
julia> FiveVector1(randn(5))
5-element MVector{5,Float64}:
0.804218
3.43472
-0.786301
0.47925
1.69257
julia> FiveVector2(randn(5))
5-element StaticArrays.SizedArray{Tuple{5},Float64,1,1}:
-1.37541
0.969937
0.364058
-0.364662
-2.35112