Bug > Swift > Enum > String Protocol
I was attempting to create an enumeration in which all its elements were file names, and I stumbled across something interesting. Like so:
enum FileNames: String {
case main = #file
}
This resulted in an internal error. (Segmentation Fault: 11)
I was able to figure how to get an actual error message:
enum Foo: String {
case one = "\(1)"
}
Error: Raw value for enum case must be a literal
Related Questions:
• Is #file considered a String literal?
• Why does #file break the enum? Should this be reported on bugs.swift.org?
• I noticed that replacing String to Int and #file to #line causes the same issue. Is this a hint?
Color Literals Don't Work
I thought they did, but I made a mistake. It also causes the same internal error.
import UIKit
enum ColorEnum: UIColor {
case foo = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
}
The Swift Programming Language (Swift 5.2)
According to Apple, #file is considered a literal:
What about nil Literals?
These also crash the compiler.
enum Foo: String? {
case breaks = nil
}
23 Characters of Mass Destruction
enum I:Int?{case a=nil}
Bug Fixed
The crash has now been fixed, it has been merged officially into Swift here: Merged on GitHub Here's the bug report: SR-12998. It has officially been implemented in Swift 5.4
Adding Support!
The use of magic literals as raw values for enum cases is being supported here: SR-13022
