To sum it up, I’m using --!strict
because i like it, but the engine is shouting at me because I’m not using annotations on metamethods.
Here is an example:
HexModule.__add = function(hex1:ANNOTATION, hex2:ANNOTATION)
return HexModule.new(hex1.q + hex2.q, hex1.r + hex2.r, hex1.s + hex2.s)
end
If I write coordinates
in ANNOTATION
, which is a type I made earlier, then the engine will shout at me whenever i try to actually use the metamethod. Here is an example:
print(hex1 + hex2)
--# Type Error: (16,8)
--# Type '{ @metatable HexModule, coordinates }'
--# could not be converted into 'coordinates'
--> this is a warning on the script editor, not an actual error <--
And then if I don’t use an annotation:
HexModule.__add = function(hex1, hex2)
return HexModule.new(hex1.q + hex2.q, hex1.r + hex2.r, hex1.s + hex2.s)
end
--# Type Error: (34,23) Unknown type used in + operation;
--# consider adding a type annotation to 'hex1'
--> there are 3 of these warnings <--
Here is the coordinates
type:
type coordinates = {
q:number, r:number, s:number
}
Which technically is true… Because that metatable is not the same as a simple table. However I can’t find a way to do this other than using: (coordinates | any)
as my ANNOTATION. I have more than one metamethod and that is really bulky.
It’s also worth mentioning that this is not an issue with every metamethod, only those which require the use of the table (q / r / s)
, like __add
or __sub
– __tostring
isn’t bothered by this even though I am using self.q / self.r / self.s
I feel like this is barely talked about and it’s quite interesting - which makes it almost impossible to find any information or people having the same issues.
Thanks to everyone for their patience, if you know anything let me know.
-Masak