yab | yet another Basic for HAIKU
variable scope and library files. - Printable Version

+- yab | yet another Basic for HAIKU (https://yab.orgfree.com/forum)
+-- Forum: General discussion (https://yab.orgfree.com/forum/forumdisplay.php?fid=8)
+--- Forum: yab stuff (https://yab.orgfree.com/forum/forumdisplay.php?fid=10)
+--- Thread: variable scope and library files. (/showthread.php?tid=42)



variable scope and library files. - bbjimmy - 10-20-2015

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.


RE: variable scope and library files. - clasqm - 10-21-2015

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.


RE: variable scope and library files. - bbjimmy - 10-21-2015

Interestingly, the scope is the same as when run as a script if one binds the program rather than using the BuildFactory.


RE: variable scope and library files. - bbjimmy - 10-23-2015

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!


RE: variable scope and library files. - clasqm - 10-24-2015

Good luck.


Free Web Hosting