2

I would like to have the following two enums. However, the below code fails to compile due to the "duplicate definition" of LargestMagnitude and SmallestMagnitude.

enum SymmetricWhich {
    LargestMagnitude,
    SmallestMagnitude,
    LargestAlgebraic,
    SmallestAlgebraic,
    BothEnds,
}

enum NonsymmetricWhich {
    LargestMagnitude,
    SmallestMagnitude,
    LargestRealPart,
    SmallestRealPart,
    LargestImaginaryPart,
    SmallestImaginaryPart,
}

How can I avoid this duplicate definition? Is there any way without needing to rename enum values within one of them? I considered the possibility of moving the duplicated values to a third enum (given below as CommonWhich), hoping I could then "derive" from is as a base class, but it is not clear to me if (or how) Rust supports this.

enum CommonWhich {
    LargestMagnitude,
    SmallestMagnitude,
}

What is the best way to proceed?

Jim Garrison
  • 4,199
  • 4
  • 25
  • 39
  • possible duplicate of [Can I extend an enum with additional values?](http://stackoverflow.com/questions/25214064/can-i-extend-an-enum-with-additional-values) – huon Aug 10 '14 at 08:46
  • @dbaupp my real question was how to avoid the duplicate definition; extending the enum was my attempt at a solution. ChrisMorgan answered me below -- the best way to proceed is to put each enum in a separate module. – Jim Garrison Aug 10 '14 at 16:20

1 Answers1

4

There is not at present any such subtyping in enums; all variants are of exactly one concrete type and what you are trying to do is impossible.

You will need to either rename variants so that they remain disjoint, or else place the enums in different modules.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215