Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to declare a static variable in a procedure, function, or method #24583

Open
woodnt opened this issue Dec 29, 2024 · 3 comments
Open

Comments

@woodnt
Copy link

woodnt commented Dec 29, 2024

Summary

The ability to declare a variable as static for a proc etc so global variables don't have to be used and thus stored in the BSS segment affecting efficiency. Closures could be used, at the expense of readability code complexity. Since static-type ability is present in C and C++ (and a closure could be implemented behind the scenes in Javascript if that is needed), if would seem not a great hurdle to add to nim-lang.

Description

This is different from using the global pragma, which is to be avoided to keep the variable out of the BSS segment, which is where globals go.

Alternatives

No response

Examples

proc myCounter(): int = 
  static var c = 0 # only initialized once
  c = c + 1
  return c

Backwards Compatibility

No response

Links

No response

@Araq
Copy link
Member

Araq commented Dec 29, 2024

I don't understand the difference to the .global pragma.

@nocturn9x
Copy link

I don't understand the difference to the .global pragma.

Do static variables in C go in the BSS segment? I don't recall, but I think the semantics around globals vs static locals is different.

@litlighilit
Copy link
Contributor

litlighilit commented Dec 30, 2024

Do static variables in C go in the BSS segment?

Initialized ones go in DATA segment while Uninitiated go in BBS.

but I think the semantics around globals vs static locals is different.

At least currently, the .global variable without a value assigned is in .bbs and that with a value is in .data.

Though not on purpose, .global does extractly what static does.

And anyway, this's about adding an extra pragma targeting only C like backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants