-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes on swift42 break attributeValueScalarType
for enumerations
#376
Comments
@radianttap This code for |
On the one hand, implementing #375 would cover this if we required enumerations to support |
…w types. Intended to fix issue #376
I just pushed 3f3eb0f which should handle this. Feedback welcome-- this branch hasn't been merged yet, after all. That commit adds a new
The generated code will look like this: public var eventType: EventType? {
get {
let key = Event.Attributes.eventType
willAccessValue(forKey: key)
defer { didAccessValue(forKey: key) }
guard let primitiveValue = primitiveValue(forKey: key) as? EventType.RawValue else { return nil }
return EventType(rawValue: primitiveValue)
}
set {
let key = Event.Attributes.eventType
willChangeValue(forKey: key)
defer { didChangeValue(forKey: key) }
guard let value = newValue else {
setPrimitiveValue(nil, forKey: key)
return
}
setPrimitiveValue(value.rawValue, forKey: key)
}
} The Swift template file is getting a little hairy, but that's really nothing new. If I can learn the template language a little better I might be able to clean it up (I see there's a "procedure" command that's starting to sound useful but I don't know how it works yet). In parallel with these changes I've been working on some updated documentation. I'll also be covering changes in this branch as part of my try! Swift NYC 2018 talk next month. |
The final implementation of the generated conversion here is great, equivalent to what I did with CoreDataRepresentable. I like the use of Not sure do we need the name to be that specific, maybe just One specific improvement I have to satisfy is that my protocol has (Looking forward to watch the Try! Swift talk, I hope video is available soon). |
The changes in the
swift42
branch are doing a lot for Swiftiness but they currently break theattributeValueScalarType
when the type is an enumeration. With a trivial enumeration such asThe generated code for an accessor using
attributeValueScalarType
=EventType
will beThis will incorrectly use
setPrimitiveValue
with a value of the enumeration type. The code will crash because the value isn't representable in Objective-C (even though the enum is declared@objc
).In contrast,
master
currently generates the following:This works as one would expect.
The new code generation was added to support optional scalars, which is a good thing. However it breaks this existing behavior.
The obvious fix is to use
rawValue
to convert to/from the enumeration type in the generated code, however that would break non-enumeration types that don't have arawValue
. For example the trivialtypealias MyType = Uint16
should work withattributeValueScalarType
, but would be broken by code that assumed it could userawValue
.It may be that a separate user info key is needed to handle Swift enums.
The text was updated successfully, but these errors were encountered: