swift-macro-toolkit icon indicating copy to clipboard operation
swift-macro-toolkit copied to clipboard

Enum inferred raw value

Open Amzd opened this issue 5 months ago • 1 comments

Previously we could already get the raw value of a case with EnumCase.value and now that can also return a new case called inferredRawValue.

enum Foo: String {
  case bar // implicit raw value of "bar"
}
enum Foo: Int {
  case bar = 3
  case baz // implicit raw value of 4
}

EnumCaseValue.rawValue and EnumCaseValue.inferredRawValue have the same associated value type so you can do:

switch value {
  case .rawValue(let initializer), .inferredRawValue(let initializer): 
    // ..
  default: break
}

This PR only adds raw representable checking when it is the first inherited type, which is the simplest case. It does not check eg typealias RawValue = String.

Amzd avatar Jul 24 '25 19:07 Amzd

Requires #31

Amzd avatar Jul 27 '25 11:07 Amzd