Issues with typechecking

Hi !

So i need some help i cannot figure out this typechecking issues mabey because they don’t exist or i just haven’t found out about them but heres the problem:

When using generic types and trying to type them like saying its generic but only this type of generic… heres example

function test<a>(b:a) -- normally
b[1] = 2 -- here it will issue that table expected but got a!
-- i understand that its comming from the a that could be anything but
--in this function i need it to be a generic type of table
--is there a way to achieve that?
end
--here is what i want but won't work because you cannot use : on generics
function test<a:{any}>(b:a)
b[1] = 2 --note that this is a example and not real code just wanted to put it out there before
--someone quotes me on it
end

2nd problem

Trying to name a pack of return arguments instead of (number,number) i wanna use (x:number,y:number) but it won’t allow because it thinks im referring to a function

--what i have been using so far
type foo = ()->(number,number) -- what is allowed
--what i want to achieve
type foo = ()->(x:number,y:number) -- what is not allowed because it thinks im referring to a func

next issue:
This is also a kind of generic issue but not fully also has to do with the return issue
But this one has one speciality that has me thinking when indexing it shows _:number
That means the _ can be replaced somehow i just don’t know how.

--what i can do
type callback<A...> = (A...)->()
local a:callback<number,number> --this will show later on when indexing it _:number , _:number
--is there a way to actually naming it? without the engine thinking its a function
-- what i want
type callback<A...> = (A...)->()
local a:callback<a:number,b:number>

im sure this one can be fixed if there is a way to index generic or solving of the first issue on the top.

I truly believe the must be some answer to some of them but don’t know so i wanted to go public and look for the answer i didn’t find any matching so made my own topic.

Anyone replying thank you!

1 Like

So i solved the first and the last issue by just adding one if statement cool isnt it?

local function a<r>(b:r)
if (type(b) ~= "table") then error("ERROR") end
end

the last one is similar though i had to change the A… to cb (callback) so i had to instead of putting in arguments just put the whole callback function in so it looks like this:

type callback<cb> = cb

and using the first solution just put a if statement to make sure its a function and nothing else!

but the challenge still is there can A… be named? like:

type callback<A...>=(A...)->()
local a:callback<other:BasePart>

but for now i got those 2 solved the last one is still a mystery but seeing roblox built in functions like game.UserInputService.InputChanged:Wait() also typechecks with inputobject,boolean
So if they can’t do it im guessing its not possible this could be a good update if to add.

For the first one, this is not the intended use case of generics. Generics are meant to allow you to work over multiple types with a common functionality, i.e. {T} for a table of any one value (i.e. all number table, all string table, etc… but not string+number). What you want to do is function test(b: {any}), which tells the function that b is a table and can be indexed. As far as I know Luau generics cannot be refined like they can in other languages.

For the second one, function return values do not have names, so you just. can’t do what you’re trying to do. If you want, you can make something like () -> ({x: number, y: number}) instead, and return a table of values instead of a tuple.

The third one is also not really how generics work. A... means a variable amount of arguments of the same type. If you want to put any values, you can use the any type, or make a union type (i.e. number | string) to refine it more. If you want a callback it should probably be any... if you’re expecting it to cover a lot of domains.

for the first one i got the issue that i want it to be userdefined and thats what generics achieve but in the same time knowing what type of userdefined it is i fixed it along with the last one but only the return one left…

i guess your solution is not wrong but i wish that was possible because ->(number,number) is not really usefull when dealing with multi tuple returns what is the first one? … i think you get it but i guess we could use a dictionary in order to achieve it.

Since no one else got something better ill give you the solution congrats!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.