Luau doesn't recognize string->number coercion

local sizeConstraint = Instance.new('UISizeConstraint')
sizeConstraint.MaxSize = Vector2.new(200, 'inf')

Typechecker will tell you that “inf” is not a number. True, but not relevant in this context.

Expected behavior

Typechecker should respect that strings coerce to numbers, including shorthand strings like “inf” and “nan”.

Thank you for the report, but this is not a bug.

The Luau team generally considers these coercions a mistake in Lua, but we cannot break backwards compatibility now.

We will continue generating typechecking errors in these contexts.

1 Like

That’s a pretty bad take imo. This is literally a feature of luau.

Either way the message is wrong. If you don’t like a feature of the language then the message should be a warning that signals that you’re doing something unsupported (like the ambiguous parentheses or multi line statement warnings). It shouldn’t present itself as an actual syntax error.

Typechecking errors are not syntax errors and you can disable them by placing --!nocheck at the start of the script.

4 Likes

This isn’t technically a Lua feature, the language will only coerce strings to numbers and vice-versa at runtime when possible, otherwise it errors. Some of Roblox’s functions are designed to coerce their inputs as well, but this is not really documented nor consistent and should not be used.

In your example, there is no reason not to use math.huge instead of "inf". If you really want to use coercion for some ungodly reason, you can ascribe the string with "inf" :: any.

4 Likes

saying it’s a language feature is too much, more considering that if you type inf, a string, when math.huge is a thing, you have gone through and back and complicated your life becuase you just didn’t want to use a standard and supported constant that is in the language by default, and you rather use weird behaviour that is just not good to use at all becuase of magical constants, its like referencing enums by string instead of the Enum.KeyCode.E object you use “E”, its just terrible practice for new code and you should not do it.