Skip to content
JuliaInteropPublic

About

C-compatible enum for Julia

Topics

Resources

Stars

14 stars

Watchers

7 watching

Forks

Latest commit

 

History

72 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CEnum

Docs Build Status Codecov PkgEval version Downloads

This package provides a C-compatible enum for Julia.

julia> @enum Foo a = 1 b = 2 c = 1
ERROR: LoadError: ArgumentError: values for Enum Foo are not unique
Stacktrace:
 [1] @enum(::LineNumberNode, ::Module, ::Any, ::Vararg{Any,N} where N) at ./Enums.jl:128
in expression starting at REPL[12]:1

julia> using CEnum

julia> @cenum(Bar, d = 1, e = 2, f = 1)

julia> d == f
true

Usage

@cenum Fruit apple=1 orange=2 kiwi=2      # base type is Int32 unless the values need a wider one
@cenum Color::UInt8 red green blue=0x10   # explicit base type; red == 0, green == 1
@cenum Day begin                          # block form
    Mon = 1
    Tue
end
@cenum Flags read=1 write=read<<1 rw=read|write   # values can use earlier members

Compared to Base.@enum, a type defined with @cenum

  • allows several names for the same value (orange == kiwi);
  • can hold any value of its base type, as in C: Fruit(7) is valid even though it has no name;
  • supports ==, <, +, -, *, ÷, &, |, xor, ~, << and >> with integers and other @cenum values. Arithmetic returns plain integers, so wrap the result to get an enum back: Fruit(apple | orange).

Integer(x) (or any integer type) gives the underlying value, instances(T) lists the members (one entry per name, so aliased values repeat) and Symbol(x) gives the first name defined for the value. All types defined with @cenum are subtypes of CEnum.Cenum{BaseType}.

Credit

The original version is written by SimonDanisch in this PR.

About

C-compatible enum for Julia

Topics

Resources

Stars

14 stars

Watchers

7 watching

Forks

Releases

Used by

Contributors

Languages