julia> struct BustedType{B, T}
s::T
end
julia> BustedType{B}(s::T) where {B,T} = BustedType{B,T}(s)
julia> t = BustedType{true}(10)
BustedType{true,Int64}(10)
julia> t2 = BustedType{false}(10)
BustedType{false,Int64}(10)
julia> testval(w::BustedType{true}) = print(w.s)
testval (generic function with 1 method)
julia> testval(w::BustedType{false}) = print(-1 * w.s)
testval (generic function with 2 methods)
julia> testval(t)
10
julia> testval(t2)
-10
julia> @which testval(t)
testval(w::BustedType{true,T} where T) in Main at REPL[5]:1
julia> @which testval(t2)
testval(w::BustedType{false,T} where T) in Main at REPL[6]:1
julia> struct BustedType2{B, s} end
julia> t = BustedType2{true,10}()
BustedType2{true,10}()
julia> t2 = BustedType2{false,10}()
BustedType2{false,10}()
julia> @generated function testval(::BustedType2{B,s}) where {B, s}
if s < 10 && B
return :(print(1))
elseif s >= 10 && B
return :(print(s))
else
return :(print(-s))
end
end
testval (generic function with 3 methods)
julia> testval(t)
10
julia> testval(t2)
-10
Note that when you make some field a type parameter, you can no longer enforce its type to be a subtype of Integer
for example so nothing stops you from making a BustedType2{"A", "B"}
which will error if comparing "B" < 10
.