M
Modular2mo ago
nairby

Is there something I’m not understanding about the Mojo memory model?

I hope this is an easy question and someone will know the obvious thing I'm doing wrong. I have this minimal example code that demonstrates an issue I’m having. main.mojo:
from command_line_mod import process_command_line_args

fn init_system():

process_command_line_args()

fn main():
init_system()
from command_line_mod import process_command_line_args

fn init_system():

process_command_line_args()

fn main():
init_system()
command_line_mod.mojo:
var bundle_file_name: String = "test"

fn process_command_line_args():

print("bundle_file_name = ", bundle_file_name)
var bundle_file_name: String = "test"

fn process_command_line_args():

print("bundle_file_name = ", bundle_file_name)
If I build and run this with mojo build main.mojo; ./main I get a segmentation fault. Why?
10 Replies
ModularBot
ModularBot2mo ago
Congrats @nairby, you just advanced to level 1!
sb
sb2mo ago
Global variables are currently a bit of a sharp edge and aren't really supported 🙃 It's not an intentional omission, it's just a feature that needs some work to make happen that should work eventually roughly "as-is" once the feature lands
nairby
nairbyOP2mo ago
Ah that explains why some of the global var use was working and some wasn't. Thanks
nairby
nairbyOP2mo ago
Is there a rough timeline of when that might expected to be supported? Is this poor program design and therefore low priority? Considering migrating a Fortran application to mojo (in the future) so my ideas on storing data in global vars in modules may be outdated
sb
sb2mo ago
globals are definitely still useful, they are just difficult to deal with and init at times so it hasn't been at the top of the priority list yet I think you can try moving stuff into a "globals" object you pass around through the call tree
sora
sora2mo ago
And if your global data is imutable, you could use alias
sb
sb2mo ago
depending on how you treat it you can avoid most issues with the borrow checker
nairby
nairbyOP2mo ago
Hmm ok interesting, I'll have to look into that more. Thanks again
sora
sora2mo ago
saying monad without saying monad?
sb
sb2mo ago
:squints: oh, yeah, huh lol
Want results from more Discord servers?
Add your server