samyeyo / LuaRT

Open source Windows programming framework for Lua
https://www.luart.org
Other
285 stars 17 forks source link

Possible bug in Directory.list() example #214

Open cosme12 opened 1 month ago

cosme12 commented 1 month ago

Hi, I was trying to list all files in a directory. Looking at the documentation example here: Directory.list() when the type() is checked it always returns table. For both, the type of File and Directory is table, so everything falls under the else condition. Hope I explained myself.

-- iterate through all current directory content
for entry in each(sys.Directory("."):list("*.*")) do
        print(type(entry))              -- Added for debugging -> Always returns table, so everything is a "directory"
    if type(entry) == "File" then
        print(entry.name)
    else -- entry is a directory
        print(entry.fullpath:match("\\.*\\([%w%p]+)$").."\\")
    end
end

Any idea how I could check the "real" type? Thanks!

samyeyo commented 1 month ago

Running this code in LuaRT Studio will show all object instances types as a "table" when using the type() function, because LuaRT Studio keeps compatibility with all Lua 5.4 compatible interpreters.

If you run this example from the command line, it will work as expected (it will print "File" or "Directory" as expected)

To check instances type, it's better to use the is() function (working in LuaRT Studio and from the command line) :

for entry in each(sys.Directory("."):list("*.*")) do
    if  is(entry, sys.File) then
        print(entry.name)
    else -- entry is a directory
        print(entry.fullpath:match("\\.*\\([%w%p]+)$").."\\")
    end
end