Enum subset or subgroup in C#
In the end, I had to rewrite much of the code but the following "trick" was derived:
I trashed C# enums and use static members on a regular class. This class was made into a singleton and is inited on application start.
My static members' constructors are allowed to reference another static member as a "parent".
Next, my init method uses reflection to go through each static member and indexes them based on several properties. These indexes are stored in hashtables which are also members of the singleton.
I thus get:
a singleton object which:
- has static members which can be easily accessed during design time.
- can be used during run-time to lookup certain static members (based on "group" and other properties).
My init method does a fair amount of validation. If invalid (such as duplicate) static members are built, you get a run-time error on application startup.
Obviously a pretty big hack but I'm quite happy with it.
Subset' of Enum values in Java
You can use an EnumSet. For example:
Set<ANGLE_TYPE> allowed = EnumSet.of(RAD, DEG);
Can an enum contain another enum values in Swift?
Details
- Swift 4, 3
- Xcode 10.2.1 (10E1001), Swift 5 (Last revision of this post)
Solution
enum State {
case started, succeeded, failed
}
enum ActionState {
case state(value: State), cancelled
}
class Action {
var state: ActionState = .state(value: .started)
func set(state: State) { self.state = .state(value: state) }
func cancel() { state = .cancelled }
}
Full Sample
Do not to forget to paste the solution code
import Foundation
extension Action: CustomStringConvertible {
var description: String {
var result = "Action - "
switch state {
case .state(let value): result += "State.\(value)"
case .cancelled: result += "cancelled"
}
return result
}
}
let obj = Action()
print(obj)
obj.set(state: .failed)
print(obj)
obj.set(state: .succeeded)
print(obj)
obj.cancel()
print(obj)
Result
//Action - State.started
//Action - State.failed
//Action - State.succeeded
//Action - cancelled
Only allow a subset of an enum as return value - OR How to get the compiler to alert me ? in C++
How about going with approach #1 but staying safe and expressing the equivalence of the enum members?
enum generic_error {
ERR_OK,
ERR_NOMEM,
ERR_IO,
ERR_GENERIC
};
enum first_error {
FIRSTERR_OK = ERR_OK,
FIRSTERR_NOMEM = ERR_NOMEM
};
enum second_error {
SECONDERR_OK = ERR_OK,
SECONDERR_IO = ERR_IO
};
int main()
{
enum first_error f = FIRSTERR_OK;
enum second_error s = SECONDERR_OK;
assert(f == s); // never fires
return 0;
}
Not that this would be particularly good practice, though... If I were you, I would really just opt to have one common error code enumeration.
Related Topics
Enable + Disable Auto-Layout Constraints
Is Self Captured Within a Nested Function
Accessing a String Enum by Index
Perform a Segue Programmatically
Optionals VS Throwing Functions
Nspredicate with Swift and Core Data
Subclassing VS Extension in Swift
How to Record Video in Realitykit
How to Fix Error: This Class Is Not Key Value Coding-Compliant for the Key Tableview.'
Accessing Self from Instance Properties Which Are Closures
How to Install Package in Xcode via Swift Package Manager
Hide Tab Bar in View with Push
Should Iboutlet Be Weak or Strong Var
Swift - Lazy Var VS. Let When Creating Views Programmatically (Saving Memory)