Skip to content

Conversation

@Amzd
Copy link
Contributor

@Amzd Amzd commented Jul 24, 2025

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.

Copy link
Owner

@stackotter stackotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Just a few small requested changes, but looks good for the most part

@Amzd Amzd requested a review from stackotter July 27, 2025 10:46
@Amzd
Copy link
Contributor Author

Amzd commented Jul 27, 2025

Requires #31

Copy link
Owner

@stackotter stackotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing those changes. Looking pretty good now, just got a few more requests. Thanks for catching and fixing the floating point issue too 🙏

}

/// Raw representable conformance is only synthesized when using integer literals (eg 1), not float literals (eg 1.0).
func previousValue() -> Int? {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this helper function to the top of the function to make things read a bit nicer.

Comment on lines +15 to +17
case .string: .inferredRawValue(.init(value: "\"\(raw: _syntax.name.text)\"" as ExprSyntax))
case .character: nil // Characters cannot be inferred
case .integer, .floatingPoint: .inferredRawValue(.init(value: "\(raw: (previousValue() ?? -1) + 1)" as ExprSyntax))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the values (after the colons) onto new lines to reduce line length and make things a bit easier to skim

public var value: EnumCaseValue?

/// Helper that gets the associated values from `EnumCase.value` or returns an empty array.
public var associatedValues: [EnumCaseAssociatedValueParameter] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be renamed to make it clear that it's the types of the associated values and not actual values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To what?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe associatedValueParameters? (given that they include labels as well)

}

/// Helper that gets the raw or inferred raw value text, eg "value", 1, or 1.0.
public var rawValueText: String? {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this essentially equivalent to rawValue?.description with a bit of added normalisation? I feel like in situations where you'd use this, you don't need the normalisation anyway, so rawValue?.description would suffice (in which case this property shouldn't be necessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea that was possible so just my inexperience with swift syntax showing.


/// Enum raw values can be strings, characters, or any of the integer or floating-point number types.
public enum EnumRawRepresentableType {
case string(syntax: IdentifierTypeSyntax)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every case has the same associated value, so I feel it may be better to turn this enum into a struct with a nested Kind enum (containing these cases but without associated values) alongside kind and identifier fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants