Skip to content
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

Ints greater than equal to 2^5 cannot be decoded as an Int when part of a Union #42

Open
johnliedtke opened this issue Feb 13, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@johnliedtke
Copy link

johnliedtke commented Feb 13, 2025

Hey pkl-swift team,

I have another issue with the union type. It appears integers that exceed 2^5 are decoded as Swift.UInt8, Swift.UInt16, etc. depending on their size rather than a Swift Int which causes the decoding to fail.

Example:

module Example

typealias IntOrFloat = Int|Float

valueOne: IntOrFloat
valueTwo: IntOrFloat
amends "Example.pkl"

valueOne = 127 // yay!
valueTwo = 128 // decoding error
Error: typeMismatch(glass.Example.IntOrFloat, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected type IntOrFloat, but got Optional(AnyHashable(128))", underlyingError: nil))

Actual type is

(lldb) po type(of: decoded!.base)
Swift.UInt8
@bioball bioball added the bug Something isn't working label Feb 17, 2025
@bioball
Copy link
Contributor

bioball commented Feb 17, 2025

I can reproduce this.

Thanks for the bug report!

As a workaround for now, you can adjust the generated files with:

-case let decoded as Int:
-    self = IntOrFloat.int(decoded)
+case let decoded as any BinaryInteger:
+    self = IntOrFloat.int(Int(decoded))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants