Been snooping on Luau.org, and I'm confused now

I hope this is the correct category; if not, please inform me
Reason I’m asking here is because Luau originated from Roblox, and I thought maybe someone here might have some good insight?

I’ve been snooping around on the Luau website, and I’ve found a few things that I find confusing.

I’ve been in Lua, specifically Luau, for just shy of a year, and have learned most basic concepts that you use in studio, like self, APIs, etc. However, I’m genuinely baffled by a few things, and now I’m lost as to how to proceed learning.

A good example of this would be generic type aliases:

function reverse<T>(a: {T}): {T}
  local result: {T} = {}
  for i = #a, 1, -1 do
    table.insert(result, a[i])
  end
  return result
end

This syntax confuses me, and I’m not sure how I’d ever practically use it.
They also have tagged unions, which baffle me:

type Ok<T> = { type: "ok", value: T }
type Err<E> = { type: "err", error: E }
type Result<T, E> = Ok<T> | Err<E>

There’s also other stuff that kind of confuse me further, but I can’t really think of what exactly they are off the top of my head
If anyone has any clue how to explain some of the concepts on Luau.org, that’d be very nice, because man am I lost! I’ve tried reading some of what the site says, but some of it just doesn’t quite make any logic sense to me.

they are useful if you need them, you probably wont come across this unless you wish for more intelligent autocompletion

They are mainly for intellisense, Generics allow you to dynamically take in any type you provide them.
For example, you can create an Array type, that will dynamically be an Array of whatever type you give it to be with Generics.

type Array<T> = {T}

local numberArray: Array<number> = {0, 1, 2, 3}
for i, v in numberArray do
	-- i: number
	-- v: number
end

local stringArray: Array<string> = {"0", "1", "2", "3"}
for i, v in stringArray do
	-- i: number
	-- v: string
end

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