Can I extend an enum with additional values?

An enum can’t be directly extended, but you use the same composition trick one would use with structs (that is, with a struct, one would have a field storing an instance of the ‘parent’).

enum Base {
    Alpha,
    Beta(usize),
}

enum Extended {
    Base(Base),
    Gamma
}

If you wish to handle each case individually, this is then used like

match some_extended {
    Base(Alpha) => ...,
    Base(Beta(x)) => ...,
    Gamma => ...
}

but you can also share/re-use code from the “parent”

match some_extended {
    Base(base) => base.do_something(),
    Gamma => ...,
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)