yab | yet another Basic for HAIKU

Full Version: variable scope and library files.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The scope of variables in library files change when the program is compiled. Run as a script, the variable scope is limited to the library file. once compiled, the scope increases to include the whole project unless they are declared local.

exmple:

test.yab...


#!yab
import library
for x=1 to 12
y=multiplybyfive(x)
print y
next


library file. ( library.yab)


export sub multiplybyfive (number)

y=0
for x=0 to 4
y = y + number
next
return y
end sub


Run test.yab as a script and all is well. Run it as a compiled binary and it hangs.


change the library like this:

export sub multiplybyfive (number)
local x,y
y=0
for x=0 to 4
y = y + number
next
return y
end sub

now it runs properly both as a script or compiled.
Thanks for the warning. Explicitly making variables local is the right thing to do in any case. You never know when someone (even yourself) grabs your library code as a snippet and just cuts and pastes it into his own program . But the discrepancy between running a script and running as a binary is a little disconcerting.

Finding out what causes this and fixing it is the long-term solution (don't look at me, way above my pay grade), but in the meantime perhaps you can include this in the documentation for the next release.
Interestingly, the scope is the same as when run as a script if one binds the program rather than using the BuildFactory.
Apparently this was a design choice:

BuildFactory.yab:

// writing all the libs into the mainfile
// because the BuildFactory can not handle libs

I think I am going to experoment with changing this and making the BuildFactory use the same method that bind uses. Wish me luck!
Good luck.
Free Web Hosting