Granular Imports#214
Conversation
Expanded the summary and design sections, added examples for granular imports, and outlined drawbacks and alternatives.
Clarified the phrasing regarding first-class variables in imports.
|
I support this. i think it's worth taking another look at this |
Corrected the phrasing from 'repeated prefixing' to 'repeatedly prefixing'.
Destructuring syntax has been a hot topic throughout the existence of Luau. It has been requested two times, and they were denied twice. The reasoning behind them was the lack of predictability and concern for Luau’s identity. It’s getting harder to maintain an identity once you’ve recognized the need for convenience and tooling. Hence, this is why such features like const, classes, and exports are entering the Luau workspace. Also, leaving export on its own isn’t very predictable. Once you have export, you’ll need a polarizing keyword to leave it symmetrical. At this point, I would rather have a full ESM-style control on Luau. This addition would be pragmatic, and it leaves less repetition for eye-strain and helps writers track their code easier without the incompetency of repeatedly prefixing commonly used variables as stated in the document. It’s a big leap if we’re asking the question of human-readability. |
|
This opens the discussion for a potential structure. Will import be static and top-level? It makes sense to separate require and import as they both serve totally different audiences. Imports may be used for importing external static typing without prefixing. I think that import would make sense as static, while require() would be dynamic. Import could serve as gathering critical utilities and components while requiring will handle dynamic objects like skills and assets. |
|
The proposed syntax is not backwards compatible or parsable. import { foo } from "bar" is valid syntax today. |
|
This feels like a needless change. you call require() "visual strain" yet how is your proposed solution any less eye straining? |
|
I understand the premise of a different programming language having different syntax than what you are used to resulting in an "unlearning process" that takes some time to get used to. However, this problem does not exist for programmers who already are proficient in Luau and a change like this would create needless friction for existing developers. |
Discussions about adding That RFC #103 also proposed an import myname, type mytype, myname2, type mytype2 from "mymodule"However, I think adopting destructuring imports with a TypeScript/JavaScript-style syntax would be a better approach. Instead of writing: const Module = require("./Module")
const A = Module.A
const AnotherB = Module.B
type U = Module.U
type V = Module.Vyou could simply write: import { A, B as AnotherB, type U, type V } from "./Module"Or, if you want to import everything: import * from "./Module"
print(A, B)
const u: U = "U"
const v: V = "V"It's much more concise and significantly easier to read. More than anything, now that the Handling everything with |
|
There is another practical reason why In Luau, when writing modules, it is common for the value returned by a module to have the same name as the module itself. src/
Car/
init.luau -- returns Car
Bus.luau -- returns BusFor example, if const Car = require("./")
const Passenger = require("../Entity/Passenger")
const Bus = {}
Bus.__index = Bus
export type Bus = setmetatable<{
Passengers: {Passenger.Passenger}
}, typeof(Bus)> & Car.Car
function Bus.new(): Bus end
function Bus:GetNearestCar(): Car.Car end
return BusOne issue here is that types such as A common workaround is to export the type under a generic name such as Another workaround is to create local aliases: type Car = Car.Car
type Passenger = Passenger.Passenger
export type Bus = setmetatable<{
Passengers: {Passenger}
}, typeof(Bus)> & CarThis works, but it adds extra declarations purely to avoid repetitive type names. The import { type Car } from "./"
import { type Passenger } from "../Entity/Passenger" |
This RFC proposes syntax for allowing named imports
Rendered view