BlockEnums.BlockEnum
— TypeBlockEnum{T<:Integer}
The abstract supertype of all enumerated types defined with @blockenum
.
Base.instances
— Methodinstances(t::Type{<:BlockEnum})
Return a Tuple
of all of the named values of t
.
BlockEnums.addblocks!
— Functionaddblocks!(t::Type{<:BlockEnum}), nblocks::Integer)
Add and initialize nblocks
blocks to the bookkeeping for t
. The number of active blocks for the type is returned.
BlockEnums.basetype
— Methodbasetype(V::Type{<:BlockEnum{T}})
Return T
, which is the bitstype whose values are bitcast to the type V
. This is the type of the value returned by Integer(x::V)
. The type is in a sense the underlying type of V
.
BlockEnums.blockindex
— Functionblockindex(v::BlockEnum)
Get the index of the block of values v
belongs to.
BlockEnums.blocklength
— Functionblocklength(t::Type{<:BlockEnum})
Get the length of blocks that the range of values of t
is partitioned into.
BlockEnums.blockrange
— Methodblockrange(t::Type{<:BlockEnum}, blockind)
Return the range of values of t
in block number blockind
.
BlockEnums.compact_show
— Functioncompact_show(t::Type{<:BlockEnum})
Return true
if compact show was set when t
was defined. This omits printing the corresponding integer when printing. To enable compact show, include the key/val pair compactshow=true
when defining t
.
BlockEnums.getmodule
— Functiongetmodule(t::Type{<:BlockEnum})
Get the module in which t
is defined.
BlockEnums.maxvalind
— Functionmaxvalind(t::Type{<:BlockEnum}, block_num::Integer)
Return the largest index for which a name has been assigned in the block_num
th block t
. This number is constrained to be within the values in the block.
BlockEnums.namemap
— Functionnamemap(::Type{<:BlockEnum})
Return the Dict
mapping all values to name symbols. Perhaps this should not be advertized or exposed.
BlockEnums.numblocks
— Functionnumblocks(t::Type{<:BlockEnum{T}}) where T <: Integer
Return the number of blocks for which bookkeeping has been set up. The set of values of t
is the nonzero values of T
, which is typically very large. Bookkeeping of blocks requires storage. So you can only set up some of the blocks for use.
BlockEnums.setblocklength!
— Functionsetblocklength!(t::Type{<:BlockEnum}, block_length)
Set the length of each block in the partition of the values of t
. This can only be called once. In order to use the blocks you must first set up bookkeeping by calling addblocks!
. These blocks are called "active".
BlockEnums.val
— Methodval(x::BlockEnum{T})
Return x
bitcast to type T
.
BlockEnums.@blockenum
— Macro@blockenum BlockEnumName[::BaseType] value1[=x] value2[=y]
Create an BlockEnum{BaseType}
subtype with name BlockEnumName
and enum member values of value1
and value2
with optional assigned values of x
and y
, respectively. BlockEnumName
can be used just like other types and enum member values as regular values, such as
Examples
julia> @blockenum Fruit apple=1 orange=2 kiwi=3
julia> f(x::Fruit) = "I'm a Fruit with value: $(Int(x))"
f (generic function with 1 method)
julia> f(apple)
"I'm a Fruit with value: 1"
julia> Fruit(1)
apple::Fruit = 1
Values can also be specified inside a begin
block, e.g.
@blockenum BlockEnumName begin
value1
value2
end
BaseType
, which defaults to Int32
, must be a primitive subtype of Integer
. Member values can be converted between the enum type and BaseType
. read
and write
perform these conversions automatically. In case the enum is created with a non-default BaseType
, Integer(value1)
will return the integer value1
with the type BaseType
.
To list all the instances of an enum use instances
, e.g.
julia> instances(Fruit)
(apple, orange, kiwi)
It is possible to construct a symbol from an enum instance:
julia> Symbol(apple)
:apple