Luau Type Checking Beta!

It’s clearly following in the footsteps of an Object-Oriented Programming language, I could assume the goal is to eventually make a class hierarchy (like polymorphism).

What’s the purpose of making Nonstrct stricter? I don’t see the purpose other than possibly making the scripts more optimized by giving more precise code.

Thanks for clarifying my point.

3 Likes

Unsure if this has been reported, but the following code gives a cryptic error:

--!strict
type A = { foo: number }
type B = { foo: number }

function foo(x: A | B)
	return x.foo
end

…gives the following warning on x.foo:

image

Would you be able to have 2 functions of the same name, but take different types? Much like how C++ class methods work, then error if there is no function of that name with the correct type syntax?

2 Likes

That’s actually called function overloading and it would great if Roblox added it to luau.
image
https://en.wikibooks.org/wiki/Computer_Programming/Function_overloading

3 Likes

This has a name; Overloading, and yes, i would it add but i think that it will be out with the Generics. I know this from Minecraft Modding (the reason why i am not very active in the community) and it exist in many Programming languages, like Java. I only want to know how we manually use it.

@JoelBrd @Eternalove_fan32 @vivivio

I actually asked this a while back, and I got this reply:

2 Likes

Ok, dont know if Roblox will ever add it, but can the Roblox team add the Ternary Operator system? It would save us a lot of time by simply write:

local string = "Its a example"
local String = "Its a example"
string == String ? "Roblox will add it!" : "Nope, it will never be true"

And not:

local string = "Its a e."
local String = "Its a e."

local function check()
    if string == String then
        return true
    else
        return false
    end
end
1 Like

The standard Lua solution to get the same result is:

return string == String and true or false
3 Likes

The conditional operator is more of a feature for “curly bracket” languages like C++, JavaScript, etc. Currently you can replicate this with a and b or c. However it messes up when b is falsey. "hi" ? false : true evaluates to false, but "hi" and false or true evaluates to true because of the nature of the and and or operators.

Btw a == a and true or false is redundant since the == operator always returns true or false

2 Likes

It’s been over a month and I’ve gotten no word on this bug, and it has not been fixed:

sorry for the month-late response…

The reason why the spawn function warns like it does is because the annotation for the inside function has to return nil instead of nothing, which is redundant:

-- current correct way of using spawn:
spawn(function() => nil -- or `nil?`
	print("stuff")
end)

The type surface from zeuxcg shows this:

-- somewhere down there,
-- inner fn annotation returns nil explicitly
local spawn: ( ()=>(nil) ) => (nil)

-- should be:
local spawn: ( ()=>() ) => ()

It would make more sense for literal nothingness and nil to be identical in type like how vanilla Lua treats it.

4 Likes

Might have something to do with userdatas not being fully supported yet. The editor believes that tables are the only objects you can call methods on or set values to.

I also get a similar "Not a table: ImageButton|Texture|nil" error in my __newindex metamethod:
self.Instance.Texture = v1

1 Like
setmetatable({},setmetatable({},{}))

safsafsafasfa

image

These warnings doesn’t make sense.

1 Like

I think it was because the game not recognize that it are empty tables.

Hello, sorry for annoying you, but i still have a question: If we make a Function and we want to indicate a Parameters that was a Part, how to attempt this, @fun_enthusiast?

This was my idea:

local PartReference = Instance.new("Part")

local function Test(Part: PartReference)
    --Does this work
end
--Else

local function OtherPossibility(Part: Instance)
    --Or this was the best?
end

If i indicate that it was a Instance, it will sure work, but it not know that it was a Part. If here was a possibility to full indicate that it was a Part, how to attempt it or it was simply not possible?

It seems if you have strict mode on you can just do this:

--!strict
local Part = Instance.new("Part")
local Sound = Instance.new("Sound")

local function Test(part: Part)
	
end

Test(Sound) -- not ok
Test(Part) -- ok
1 Like

@Nezuo, i not am using strict mode, then i am new with Luau. Was here not another way to make it (Without the strict mode)?

Non-strict mode is for backwards compatibility so script without types still work. This is why a non-strict typed script does not error when you don’t pass in a Part. Strict mode makes it so it will error if the passed argument is not the right type. If you want to use types, I would suggest using strict mode.

It’s likely because functions do not necessarily return a single value. In this case it should know that setmetatable has a single result, but wrapping the final argument in parentheses should silence the warning:

setmetatable({}, (setmetatable({}, {})))

It’s inconvenient, but I recommend wrapping the final item in an array with parentheses if it’s a function call. This both improves performance, and prevents possible bugs if you decide your function should return more than one value later:

{foo(), foo()}

{foo(), (foo())}

You may also do this for returns or function call arguments, but the performance impact isn’t as significant.

Actually, are there cases where providing the results of a call like this can cause deoptimizations @zuexcg?
For example, how does this:

for i, v in ipairs(foo()) do
end

local bar = math.abs(foo())

compare to this:

for i, v in ipairs((foo())) do
end

local bar = math.abs((foo()))
2 Likes

I am kind of confused with how I would use the function type. Is it used like this?

type sumFunc = (number, number) => number

local function add: sumFunc(intA: number, intB: number) => number
    return intA + intB
end