Get name of the enum in scala 3? - scala

Java enum has name() method but scala 3 doesn't have it.
How to get name of the enum in scala 3?
enum Color:
case Red, Green, Blue

toString() method gives the name of the enum.
enum Color:
case Red, Blue, Yellow
println(Color.Red.toString()) //=> Red
Scala 3 enum can also extend java Enum class and use the name() method.
enum Color extends Enum[Color]:
case Red, Blue, Yellow
println(Color.Red.name()) //=> Red

Related

how to define an array of a particular enum case in swift

I want to define an enum case which is recursively defined by a particular instance of the same enum
enum Menu {
case item(String)
case submenu([.item]) // i want a submenu to be defined by an array of items
}
the submenu case isn't correct:
case submenu([.item])
how can I confine it?
All cases of an enum are of the same enum type, so you cannot declare an Array that can only hold a specific case of an enum.
However, you can achieve your goals by creating 2 enums instead of one and making the Menu.submenu enum case take the other enum as its associated value.
enum Menu {
case item(MenuItem)
case submenu([MenuItem])
}
enum MenuItem {
case item(String)
}
Then you can use it like
let menu = Menu.item(.item("main"))
let submenu = Menu.submenu([.item("a"), .item("b")])

How to get enum case name in Swift from within CustomStringConvertible implementation?

I have an enum that looks like this:
enum IntteruptCause: UInt8 {
case userDisplayDisengage = 0
case userSteeringWheelDisengage = 1
case positionDropout = 2
}
Currently the output from print() will give me the case name.
I would like to implment CustomStringConvertible and have it produce both the case name and the rawValue, e.g. the string User_Steering_Wheel_Disengage (1).
When I implement var description: String I have easy access to rawValue but I cannot seem to find out how to get the enum case name. Normally I would do String(describing: value) but that would create an infinite recursion if used in description implementation.
Is it possible to get the enum case name from within the description implementation?

How to Create inheritance for enum [duplicate]

This question already has answers here:
Swift enum inheritance
(3 answers)
Closed 3 years ago.
I am trying to create an inheritance for below enum
enum BankAuthError: String {
case authFailed = "AuthFailed"
case technicalError = "Unavailable"
case accountLocked = "Locked"
case unknownError = "UnknownError"
case userInteractionRequired = "UserInteractionNeeded"
case realmUserAlreadyConnected = "UserExists"
}
I am able to use this enum as below
let errorCode = BankAuthError(rawValue:errorMessageCodeString)
Now I am trying to create inheritance from above struct as below
//MARK:- Enum to handle all sysnc errors
enum SyncErrorStatus: BankAuthError {
case usernameOrPasswordMissing = "UsernameOrPasswordMissing"
case signatureMissing = "SignatureMissing"
case twoPhaseAuthentication = "TwoPhaseAuth"
}
But if I am doing this, I am getting the compiler error as
'SyncErrorStatus' declares raw type 'BankAuthError', but does not
conform to RawRepresentable and conformance could not be synthesized
Please let me know whether can we create inheritance from above Raw enum or not.
Enums are value types, so there's no such thing as inheritance for enums. When you declare an enum as enum YourEnum: Type, you declare the rawValue of your enum to be of type Type. However, Type needs to conform to RawRepresentable.
What you are looking for, to create an enum that contains all cases of another enum, plus some other cases in currently not possible in Swift. You cannot inherit all cases of an enum.

Swift: Can I (run-time-checked) look up a CodingKey's corresponding member value?

With a CaseIterable CodingKeys enum and Mirror I can almost implement a runtime-type-checked version of the compiler-magic Encodable implementation. Almost, but not quite, because from a CodingKey value I can't get back to the enum case name, which I need to look up the matching value via Mirror(reflecting: myEncodable).
enum CodingKeys: String, CodingKey {
case defaultName // default: .stringValue is also the myEncodable member name
case nonDefaultName = "some-other-name" // .stringValue is not the myEncodable name, no access to "nonDefaultName"
}
I would have expected to be able to find the case name somehow via Mirror(reflecting: CodingKeys.nonDefaultName) but that doesn't seem to be the case.
Am I missing something? Is this in fact possible using Mirror? (I'm still on Xcode 9, by the way, faking CaseIterable until I get the real thing.)
Any alternative ways to get to the same goal (guarantee that all members mentioned in CodingKeys get looked up on the object and fed to the Encoder)?

Multiple protocol conformace in enum doesn't work

I am trying to conform multiple protocols with single protocol in the enum. If I type them separately it work. However, if I try to combine them in a single protocol it doesn't work.
I have following enum
enum Theme:Int,CaseIterable,CustomStringConvertible {
case light
case dark
var description: String {
switch self {
case .light:
return "Light Theme"
case .dark:
return "Dark Theme"
}
}
var backgroundColor: Color {
switch self {
case .light:
return .white
case .dark:
return .black
}
}
}
I tried to convert its conformance to protocol by this
public protocol ThemeProvider where T.RawValue == Int {
associatedtype T:RawRepresentable,CaseIterable,CustomStringConvertible
}
and changed enum definition to enum Theme:ThemeProvider
However it says 'Theme' does not conform to expected type 'ThemeProvider' What is the proper way to add protocol to enum?
PS: Above code is Swift 4.2 and uses new protocol called CaseIterable
Changing the enum Definition from enum Theme: Int, CaseIterable, CustomStringConvertible { to enum Theme:ThemeProvider { does not make Theme automatically have a T, which is required by ThemeProvider.
And if you add typealias T = Theme then the Theme enum does still not have Int, CaseIterable, CustomStringConvertible, cause typealias T = Theme does not make any type(Theme here) conform any protocol.
That's why enum Theme: Int, CaseIterable, CustomStringConvertible, ThemeProvider { typealias T = Theme ... is required to make Theme conform to ThemeProvider.
What you probably want is enum Theme: Int, ThemeProvider { where ThemeProvider is public protocol ThemeProvider: RawRepresentable, CaseIterable, CustomStringConvertible {}.
More can't be said without any example.