Signed and Unsigned Integer Subtype Refinements#189
Signed and Unsigned Integer Subtype Refinements#189Crazyblox wants to merge 3 commits intoluau-lang:masterfrom
Conversation
| ```luau | ||
| local function processUnsigned(val: unsigned) | ||
| print(integer.ult(val, 100i)) | ||
| end | ||
|
|
||
| local a: integer = 10i | ||
| local b: signed = -10i | ||
|
|
||
| processUnsigned(a) -- Valid | ||
| processUnsigned(b) -- Type Error: Expected unsigned, got signed | ||
| ``` |
There was a problem hiding this comment.
Is a supposed to be annotated as unsigned? Because it doesn't make sense to allow integer in a place that expects unsigned if unsigned <: integer.
There was a problem hiding this comment.
Should be resolved, along with an additional example for clarity.
|
Not sure about the naming; EDIT: Resolved (maybe I should've made this a Review Comment...) |
|
How is -- Accepts 'integer' and its subtypes
local function processInteger(val: integer)
-- Type errors if val was passed as 'signed' integer subtype
print(integer.ult(val, 100i)) -- 'integer' refines to 'unsigned'
end
local a: integer = 10i
local b: integer = -10i
processUnsigned(a) -- Type Error: Expected unsigned, got integer
processInteger(b) -- Valid ('integer' refines to 'unsigned')intended to function? Both Something we'd also want to consider is how do constant integers infer? If we have Reading further down in the RFC actually there's
How would we represent this? I do think waving away that conceptual inaccuracy would resolve the issues suggested above, with a literal would always infer as It would be interesting to see some discussion of inference in the RFC in general. Inferring a negative constant as always Just one final edit: This is quite a wall of text, but I'm very much in support of adding proper signed and unsigned types! |
| ``` | ||
|
|
||
| #### 3. Function Overloads/Refinement | ||
| Functions can be written to accept the base `integer` type and refine it internally, or specify the refinement to constrain the input. |
There was a problem hiding this comment.
I think it's important to note that
and refine it internally
isn't possible. There's no way at runtime to ever discern between a signed or unsigned integer, and therefore once the types no longer carry that information they likewise have no reasonable checks that could be used as part of refinemt.
There was a problem hiding this comment.
Providing the equivalent of :: signed or :: unsigned within the relevant integer library functions would be sufficient to avoid runtime behaviour, which the RFC is stated multiple times to not involve.
There was a problem hiding this comment.
I was meaning more in as much as any code that takes an integer can't safely know to cast it to one or the other. I'm not suggesting types should leak to runtime, but rather that there's no runtime behaviour that could be used to determine if/how an integer should be refined. A quick ::signed or ::unsigned, in the same way you can do
int64_t foo = -1;
uint64_t bar = (uint64_t)foo;In C is fine, but I'm not sure if it's worth mentioning given that's not something that can be "safely" done, except when intentionally breaking rules (bit maths on negative numbers, etc.). Would :: signed and :: unsigned not work fine here?
Rendered