listProjectTabs outside setup function

When using listProjectTabs (or readProjectTab) before setup is run, the data is not usable until setup starts.

Can others confirm?

t = listProjectTabs("Whatever Your Current Project Is")
print(t) -- table hexref
print(#t) -- anything that actually accesses the table won't work, the code won't run

function setup()
  print(#t) -- although when t was defined it wasn't usable, it now becomes usable
end

For me, the print(#t) before setup() gives an error and the program doesn’t run. Therefore, I wouldn’t use it before setup(). Somethings can be used before setup() and somethings can’t. The purpose of setup() is to set things when the program first starts.

EDIT: Is there a reason this would be used.

@dave1707 Thank you for confirming the bug. I’ll post it on the bug tracker when I get a moment.

@LoopSpace I’m not confirming it as a bug. I just showed that it also happens for me. If I tried using print without the (), I would get an error. I wouldn’t consider that a bug, that’s just a requirement of the print statement. So is what you pointed out a bug, I don’t know. I myself wouldn’t try to define a table outside a function and use code outside a function to reference it. So for me it’s not a bug but a requirement of the editor/language. As I asked above, is there a reason for using code outside of functions. I guess you can post it as a bug and the developers can decide.

I don’t think it’s a bug. It’s not essential for print statements to work outside setup, surely?

As print is used to provide logging info to the console (duh), I can see it’s uses as a debug tool to ensure that tables for instance have the correct number of entries or that variables are initialised correctly from functions (is this allowed?)

I’m guessing that the print function fails because maybe there is some Codea runtime setup that happens for the console output to function after the lua modules are loaded / defined but before setup is called.

Just my $0.02

The first print(t) works because listProjectsTabs() returns a table and so t will be a table and that’s all the editor/compiler is checking at that point.
The first print(#t) doesn’t work because listProjectsTabs() hasn’t executed yet so the editor/compiler sees #t as undefined and flags it as an error.
When setup() runs, listProjectTabs() is executed and #t now has a valid value and the second print(#t) is valid.
If t=listProjectTabs() is replaced with t={1,2,3,4,5,6,7}, then everything is valid because the editor/compiler knows that t is a table and it has 7 entries.
So apparently listProjectTabs() doesn’t actually run until setup() is run. I think that’s also true for other things.

Try this. WIDTH and HEIGHT don’t have the correct values until setup is run.

print(WIDTH,HEIGHT)

function setup()
    print(WIDTH,HEIGHT)
end

EDIT: That’s just my guess as to how things are running.

My apologies. I wrote in haste. I should have written:

@dave1707 Thanks for confirming that the behaviour of listProjectTabs on your iPad is the same as it is on mine. I consider that a bug and I will report it on the issue tracker at my next convenience.

Incidentally, there are circumstances in which print will work without brackets.

@LoopSpace I know print can be used without () in some other languages, but Lua/Codea doesn’t seem to be one of them. Have you used print without () anywhere in Codea. Just curious if you have. I still come across things I don’t know even after all this time.

@dave1707 Try the simplest case first: print "hello world"

@LoopSpace Thanks. Apparently I never tried print without the (). It’s just natural to type print and hit the () key. The " " must be equivalent to the (). Like I said, I still come across things I don’t know. That’s what I like about this forum, there’s always something new to learn.

If the arguments to a Lua function are a single table, or a single string, you can omit the enclosing ()

Which is why you often see myFunc{} used instead of myFunc({})

On a side note - I know the triple tilde is used to mark a block of code, but what’s the markup/down to mark an line single statement?

Backwards apostrophe

`inline`

On iPad press and hold apostrophe, it’s the left-most option

Thanks :slight_smile: