Can you call “require” on a variable?

Deadly 提交于 2019-12-10 17:32:08

问题


I have a lua file that will require another lua file to run but I can't hard code the file name. Can i use the require function on a variable or do i need to figure out an alternate approach to what i am doing?

For example

local path = "mypath.txt"

local level = require path

回答1:


Yes, you can. require "module" is just a syntactic sugar for require("module") that only works if call a function with single argument that is a string or table constructor. Use proper call in form of require(path) and it will work.



来源:https://stackoverflow.com/questions/11836434/can-you-call-require-on-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!