You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First thing I did was change the type parameter constraint to Unsigned & UnsignedInteger[T] instead of just Unsigned
Unsigned is the union type that contains all builtin unsigned integers
If you use only this union as the constraint, then theoretically from the compiler's perspective you could instantiate with a union type as the type argument - for example Block[(U8 | U16)], which would cause problems because you can't do binary operations on numbers of varying types
Using the trait UnsignedInteger[T] as a constraint for T guarantees that T must be able to do those binary operations that are part of the trait with a second operand of type T, which resolves that issue.
The other relevant thing I did to fix the code was to wrap the numeric literals in some extra needed magic - changing 2 into T.fromU8
The Pony compiler can't do number literals for a generic type - it needs to know it as a single type at the time of encountering the literal. So the T.fromU8 expression tells Pony that the number literal is of type U8, then it will convert to type T.
Since we're dealing with number literals, which constant values in LLVM, the extra conversion can be trivially optimized by LLVM such that there is no runtime overhead for the conversion from U8 to T.
Once this pattern is created, we should in turn create an FAQ on the website and link to this pattern.
The text was updated successfully, but these errors were encountered:
This comes up often. Writing generic functions over number types.
For example a reasonable naive example might be:
where the correct Pony version to compile would be
As @jemc explained in Zulip, the issue is:
Once this pattern is created, we should in turn create an FAQ on the website and link to this pattern.
The text was updated successfully, but these errors were encountered: