luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau.org
MIT License
3.95k stars 371 forks source link

[FEATURE] Add optional explicit types #1373

Closed Strokkur424 closed 3 weeks ago

Strokkur424 commented 3 weeks ago

It would be really sick if you could explicetly define types for variables, as to prevent unsafe operations by accident.

Where would this be a case?

The most obvious usecase would be for variables:

local x: String = "Hey"

Another place I would put this, is into function declarations, like this:

local function someOperation(a: Number, b: Bool) 

Why?

A runtime error could be thrown if a explicetly typed variable would be:

  1. Set to an incompatible type. If one assigns a string to a number, it would throw an error
  2. If an incorrect typed variable gets put as input into the method. This would reduce unwanted behaviour.

Some more information

This would not replace the current in-set dynamic typing. It would incorporate the possibility to use both at once, if wanted. Similar to how TypeScript merely adds type checking to JavaScript, in order to enhance code readability.

Afterword

If you have questions or further ideas, please comment down below. I will respect all feedback about it. If this is seen as a positive idea, I might even try to implement it myself (but don't get your hopes up though).

aatxe commented 3 weeks ago

I'm very confused by what you're suggesting here. Luau already includes a type system that supports both automatic type inference and explicit type annotations.

If you're specifically asking about inserting runtime typechecking based on explicit type annotations, TypeScript doesn't do this either. It is possible to do, but runtime typechecking comes at considerable costs, particularly when you're dealing with higher-order functions which are reasonably common in Luau.

Strokkur424 commented 3 weeks ago

It does? And in what format? None of the documentstions I have seen about Luau mention anything regarding explicit types

Strokkur424 commented 3 weeks ago

Ohhh I just now figured out this has a website with documentation. My issues was that I only saw sources in the roblox dev forums and documentations, where they meantioned explicetly typed stuff was not a thing. And google searches did not help me in any way either.

Well, I have figured it out now. Sorry for wasting your time 😅!

Have a nice rest of your day!