lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Can I overload/Generic on mathlib functions to accept extra data types?

Open muppet9010 opened this issue 3 years ago • 5 comments

I have some custom types defined and would like to make math functions accept then and return the same data types as go in. I have these custom types as they are very helpful in distinguishing value types.

For example the type def of:

---@class uint : integer

And I would hope I could "extend" the mathlib.random() function to accept the below:

local randomUint ---@type uint
local minUint = 30 ---@type uint
local maxUint = 100 ---@type uint
randomUint = math.random(minUint, maxUint)

As at present math.random returns integer type and so it gives an assignment warning,

I can force randomUint to be viewed as a type uint with --[[@as uint]], but this then runs the risk of future code changes that lead to other type values being passed in not being warned about. I know it still relies on me not setting a variable of type uint to a negative number type value, but that is a more explicit user error than me accidentally passing in another variable and thinking it is type uint when it is actually of type integer (negative numbr).

I tried adding the below type of thing, but it still failed the validation test as the returned type can't be pinpointed to uint, from possibly also being integer from the default function def you include in the extension.

---@class mathlib
local math = {}

---@overload fun(m: uint, n:uint):uint
math.random = function(m, n)
end

I also have similar issues around adding two uint values together and them being returned as a number type. While the result could be outside a uint, tat would require me to set a uint type variable to a non uint value.

muppet9010 avatar Jul 09 '22 18:07 muppet9010

Currently only supports distinguishing prototypes based on the number of parameters

sumneko avatar Jul 11 '22 07:07 sumneko

Is there a option/setting I can use to not load the included mathlib.lua definitions you include; and then add my own version of it with generics instead of number types?

muppet9010 avatar Jul 11 '22 10:07 muppet9010

Is there a option/setting I can use to not load the included mathlib.lua definitions you include; and then add my own version of it with generics instead of number types?

runtime.builtin

sumneko avatar Jul 11 '22 11:07 sumneko

Thanks, generics looks to work for stopping the maths functions from warning. :) Although I have lost protection against invalid data types, i.e. boolean type being passed in to math.random(). It is better in the short term at least for me. But doesn't seem to be a good final solution unfortunately, or I have just made bad generic definition.

I can't find in your files anywhere that says that the "+" or metatable operator __add is of type integer to change though. Is this something you know to hand?

muppet9010 avatar Jul 11 '22 13:07 muppet9010

I can't find in your files anywhere that says that the "+" or metatable operator __add is of type integer to change though. Is this something you know to hand?

You can use

---@class uint
---@operator add(uint): uint

in next version(3.5.0), or you can use the master.

sumneko avatar Jul 11 '22 14:07 sumneko