For example, let's define a type A
, which is enumerable by B
: B1
or B2
, and also enumerable by C
: C1
or C2
.
So we can organize it by enumerating first by B
then by C
:
enum A {
B1 {
b1_common: bool,
b1_c: B1C,
},
B2 {
b2_common: bool,
b2_c: B2C,
}
}
enum B1C {
C1 {
c1_common: bool,
b1_c1: bool,
},
C2 {
c2_common: bool,
b1_c2: bool,
}
}
enum B2C {
C1 {
c1_common: bool,
b2_c1: bool,
},
C2 {
c2_common: bool,
b2_c2: bool,
}
}
Or I can reorganize it by enumerating first by C
then by B
:
enum A {
C1 {
c1_common: bool,
c1_b: C1B,
},
C2 {
c2_common: bool,
c2_b: C2B,
}
}
enum C1B {
B1 {
b1_common: bool,
b1_c1: bool,
},
B2 {
b2_common: bool,
b2_c1: bool,
}
}
enum C2B {
B1 {
b1_common: bool,
b1_c2: bool,
},
B2 {
b2_common: bool,
b2_c2: bool,
}
}
But either way, I has to duplicate some common field and some common method.
I have also considered using generic type:
enum A {
B1 {
b1_common: bool,
b1_c: C<C1B1, C2B1>,
},
B2 {
b2_common: bool,
b2_c: C<C1B2, C2B2>,
}
}
enum C<C1B, C2B> {
C1 {
c1_common: bool,
c1_b: C1B,
},
C2 {
c2_common: bool,
c2_b: C2B,
}
}
struct C1B1(bool);
struct C1B2(bool);
struct C2B1(bool);
struct C2B2(bool);
In this way, I avoid duplicating common fields. But I have some problems when implementing methods for C
. Is it a good idea?
Are there any better ways?