Setting the type as "Hello World"
means that the value should only be equal to a “Hello World” string.
Kindof, if you use nonstrict lua then it’s up to you, you can use types to describe functions, for instance:
local function Calculate(a: number, b: number): "Returns sum of a + b"
return a + b
end
Yes, you can use wrong types with non-strict Lua but it doesn’t mean it’s a right thing to do.
Hm, does it actually apply the description or it just displays the description as a type?
In my opinion, its better practice to use multiline comments to describe functions, since it actually shows up in the autocomplete
--[[
returns the sum of a and b
]]
local function add(a: number, b: number): number
return a + b
end
plus you can do more than 1 line:
--[[
returns the sum of a and b
both arguments are numbers
]]
local function add(a: number, b: number): number
return a + b
end
and if you still insist to use “” for describing functions, then you would be in a complete hell of types. since the intellisense would infer it as the exact string annotated in the function. so, for example, instead of having {state: 0, value: 10}
, you would get "returns the state and value"
, and wont get the proper autocompletion (you would get string functions like :sub
, :match
, etc… instead of .value
and .state
)
i did not know you could that! that’s really cool
i understand, but still it was simply just showcase what some people do, personally i prefer single line comment for functions
it becomes very useful in functions because it will allow for auto complete
it will show an error if you wrote a different string in strict mode but autocomplete will show in all modes
for example
type calculatorModes = "Add" | "Subtract" | "Multiply" | "Divide" -- strings that will autocomplete
local function calculator(num1 :number, num2 : number, mode : calculatorModes) : number
if mode == "Add" then
return num1 + num2
elseif mode == "Subtract" then
return num1 - num2
elseif mode == "Multiply" then
return num1 * num2
elseif mode == "Divide" then
return num1 / num2
end
end
calculator(5,5, "AutoComplete will show here")
Types aren’t under-rated at all. I use them on a daily basis and most of the experienced programmers on roblox do as well. Regardless if it’s within object-oriented enviroments or just for function input, I dislike creating function’s without using types, and I rely on custom networking solutions like ByteNet due to their strictly typed nature.
Real world example of me using types:
function Class.new(category: string, eventName: string, ...)
local self = setmetatable({}, Class)
self.eventName = eventName
self.category = category
self:__reserve()
Replication.Replication.sendToAll({
category = category,
eventID = self.eventId,
eventName = eventName,
eventFunc = "new",
props = table.pack(...),
})
print(self.eventId) -- for debugging/diagnosing memory leaks
return self
end
Still, beginners never learn about them because youtube doesn’t have any direct tutorial about them, by making this tutorial i wanted to show that this feature can be usefull to make their code better
YouTube is a generally bad platform for learning roblox scripting. Most youtuber’s don’t know what they’re doing, Don’t provide enough information, Or are just inefficient.
Still most people go there to learn, and later search on dev forum
omg please just type : any
thank you
i can’t tell if this is a joke or not, but the effort is commendable i must say