Luau Recap: February 2020

Definitely. Are records going to come to Luau, or is it already built-in without the $ syntax?

Personally, I prefer & and | for bitwise operations as well.

&& and || are only really used in languages like JavaScript or C, and Lua is not any of those.

I love this, itā€™s really nice looking. Iā€™ll be able to change how I program alot, and be very efficient with this.

Same for this.

As far as I know, && and || are commonly used for logical comparisons, not bitwise operations. Lua already has logical comparison operators that handle those tasks, so those two operators would be redundant in that use case. Lua 5.3+, JavaScript, C, C++, and many other languages use & and | for bitwise operations, so I donā€™t really see any kind of logical basis to bring && and || in for those.

1 Like

This might be a bug.

image

Any idea why this happens?

I guess itā€™s because :FindFirstChild takes two arguments, but the second one (the boolean) is optional. Or maybe because itā€™s not used to function calls without parentheses??? Does calling it with the string and false (assuming you donā€™t need it to be recursive) work?

1 Like

Hi, so I actually came across a point today where I needed to use the ā€œcontinueā€ command.

Iā€™m a little confused by it at this time.

Is the command already functional? Iā€™m not using Beta features or anything, but it didnā€™t throw an error and SEEMS to be working correctly.

Itā€™s about a month too late to be asking this, but can you briefly explain this more? Iā€™m trying to wrap my head around how type intersections were useful here and I canā€™t for the life of me do it.

Itā€™s a slightly involved concept, but it will mostly be used in our internal APIs. Still:

When a value is A | B, it means it either matches the type A or B but we donā€™t know which one. For example, string | number could be a string or a number. A different example is { a: number } | { b: number } - this means we know itā€™s a table that either has a key a or a key b but we donā€™t know which one.

When a value is A & B, it means it matches both types A and B. This may seem counterintuitive - how can this be? And indeed, some intersections are non-sensical - no value can be string & number.

However, what about { a: number } & { b : number }? This effectively means that the value has both keys a and b. This is actually nifty in some cases, e.g. Instance & { CFrame: CFrame } says ā€œa type that has all fields that Instance has, and also a CFrame fieldā€. Which might be helpful in cases where we expose two types that have a field with a common name but no common base class.

The most important use for this is modeling overloaded functions internally. A type (number) => string | (string) => string represents a function that canā€™t be called (as far as our type system is concerned) with a number or a string - because this says ā€œitā€™s either a function that takes a number, or a function that takes a stringā€ - so if you pass it a string and it actually takes a number, itā€™s not valid to call it.

However, a type (number) => string & (string) => string says ā€œvalues of this type are functions that can be called with a number and with a stringā€.

Youā€™re not really supposed to understand this, but if you really want to hopefully you can now :slight_smile:

10 Likes

Would it be possible for table types to use number names, { 1: number, 2: string } for example?

Most of my game has been refactored to use arrays to boost performance. Hereā€™s a simple class using this design:

local key_Health = 1
local key_Name = 2

return {
    new = function(health, name)
        return {health, name}
    end,
    Heal = function(self, amount)
        self[key_Health] = self[key_Health] + 1
        print("Healed", self[key_Name])
    end,
    
    -- Other modules use these to access our objects:
    kHealth = key_Health,
    kName = key_Name
}

Thereā€™s no ā€œrightā€ way to do classes in Lua. Metatables are an intuitive place to start, but simple types with static methods are always the most performant.

My gameā€™s custom compiler can completely omit the above module from my game if I flag the functions as inline, but by default it will just omit the 2 constant module keys, and collapse the rest of the module table to an array (where new ā†’ 1 and Heal ā†’ 2) for the smallest memory footprint.

Iā€™m designing a scalable MMO, and if my code uses fewer unique strings, then thereā€™s just more room to store things like encoded item and animation data.

2 Likes

To check my understanding, something like UDim2.new would be typed like (number, number, number, number) => UDim2 & (UDim, UDim) => UDim2, correct? Thatā€™s where my confusion was, and if thatā€™s right youā€™ve cleared it up. Thanks!

1 Like

Yeah thatā€™s right.

1 Like

Will luau are able to reduce amount of exploiters?

the progress is going so well, glad to see that you all are still working hard on this!

I know this is a little late, but will there ever be a method to share types between modules without requiring that module at runtime? The more I plan out how I want to use typed lua when it comes out, the more I find it absolutely essential that you can circularly share types between modules.

From what I could tell, type MyModule = typeof(require(path.to.MyModule)) was at one point valid syntax in the type checking beta which in theory allows you to get a moduleā€™s type without actually requiring it. Rut is this going to be valid syntax upon release of typed lua, and if so would it cause any performance downsides? For example, is the result of such a typeof() statement memoized, or will the script analysis have to re-infer the type of MyModule every time this statement is used?

For me, it is really important that I be able to import types from other modules without requiring those modules at runtime, and it seems like something that is entirely possible within the language, were it not for the fact that importing types (which should not affect procedures at runtime) requires you to require those typesā€™ source module at runtime.

I would really love some kind of type-only script like a ā€œTypeScriptā€ or ā€œTypeModuleScriptā€ that allows types to be exposed, without affecting anything at runtime other than performance optimizations. If there isnā€™t a straightforward method of sharing types without requiring at runtime, Iā€™m sure there would be some hacky ways to do so using setfenv to replace the functionality of require, but Iā€™d rather not go down that rabbit hole.

typeof(require(path)) should work and should be efficient.

1 Like

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