In Julia, you can’t dispatch on a value such as true or false . However, you can dispatch on parametric types, and Julia allows you to include “plain bits” values (Types, Symbols, Integers, floating-point numbers, tuples, etc.) as type parameters.
But how can one actually define such a type? I tried the following and it doesn’t seem to work.
julia> struct tree{D<:Integer}
size::Int
end
julia> t = tree{33}(15)
ERROR: TypeError: in tree, in D, expected D<:Integer, got Int64
Stacktrace:
[1] top-level scope at none:0
Your definition isn’t quite doing what you think, because D <: Integer is not the right condition. D <: Integer says that D is a type which is some subtype of Integer, not that D is a value whose type is <: Integer. For example:
julia> Int <: Integer
true
julia> Int32 <: Integer
true
julia> 33 <: Integer
ERROR: TypeError: in <:, expected Type, got Int64
Stacktrace:
[1] top-level scope at none:0
struct MyIntField{T}
myfield::T
function MyIntField(x::Integer)
return new{typeof(x)}(x)
end
end
MyIntField(x) = throw(DomainError("noninteger type"))
a = MyIntField(2)
# MyIntField{Int64}(2)
b = MyIntField(2.0)
# ERROR: DomainError with noninteger type
or
struct MyIntField{T}
myfield::T
function MyIntField(x::T) where {T<:Integer}
return new{T}(x)
end
end
MyIntField(x) = throw(DomainError("noninteger type"))
I think that is far too strong a statement.
There are many good reasons to store many things in the type parameters.
You just have to be aware of the trade-offs.
Which are roughly: Pro: you can dispatch on it, Con: a new instance of every method called on it will have to be compiled.
if you mean there is one instance of your struct and so there is only int value that is used as a parameter, yes – still not clear why you want to do it that way