覃天琛
覃天琛

Reputation: 51

Why we dont need to use "require" statement but still be able to use the build-in function?

In C language, we use #include statement and using statement in C# to be able to attach its build-in function. But in Lua, we dont need to do anything, then we can use the coroutine, table, io, etc.

Upvotes: 1

Views: 125

Answers (2)

luther
luther

Reputation: 5564

The Lua interpreter exports all basic functions and library tables to Lua programs by calling luaL_openlibs before running the program.

EDIT:

Why don't all languages do this?

It's a trade-off. If a language exposes its entire standard library by default, it saves us a lot of boilerplate. On the other hand, it can pollute the namespace, use more memory, and increase startup time. Lua's standard library is small, so it doesn't cost very much. Most compiled languages try to be as lean as possible, so they make us import everything explicitly.

Upvotes: 2

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

It's because Lua "pre-imports" some of the libraries for you. You can re-configure your copy of Lua to load a different set of libraries. See lualib.h file in the Lua distribution for the list of pre-loaded libraries.

Upvotes: 4

Related Questions