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

Module Variables #22714

Open
2 tasks done
StunxFS opened this issue Oct 31, 2024 · 4 comments
Open
2 tasks done

Module Variables #22714

StunxFS opened this issue Oct 31, 2024 · 4 comments
Labels
Feature Request This issue is made to request a feature.

Comments

@StunxFS
Copy link
Contributor

StunxFS commented Oct 31, 2024

Describe the feature

Weeks ago, on V's Telegram channel, I had proposed the idea of ​​introducing module variables.

The objective of this idea is to remove __global from the compiler completely, since module variables could replace its function (in a safer and more idiomatic way).

Global variables in V require a flag to work (-enable-globals) and can be accessed from anywhere in the code, plus they are always mutable, making them somewhat unsafe to use.

Additionally, with the implementation of module variables, you could make const stop receiving values ​​that are computed in runtime, because static would fulfill that function perfectly. Then const would receive pure values ​​that can be computed in comptime.

My suggestion is to use the static keyword (already used to declare static variables inside functions) to declare module variables, which can be declared as pub and mut as needed:

// report/report.v
module report

pub static mut errors = int(0)
// main.v
module main

import report

fn main() {
    report.errors += 1
}

Use Case

In addition to what has already been said, the idea is also to make a distinction for constants, which currently receive all types of values ​​(both runtime and comptime), which could confuse new users who are learning V, but come from other languages ​​where const receives pure values ​​that are computed in comptime.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

latest

Environment details (OS name and version, etc.)

anywhere

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@StunxFS StunxFS added the Feature Request This issue is made to request a feature. label Oct 31, 2024
@jorgeluismireles
Copy link

Like a singleton pattern? Would be able to be shared among threads?

@medvednikov
Copy link
Member

I like the idea of consts being comptime only.

I don't like having mutable globals in the language, not hidden behind ugly keyword and a compiler flag.

@Wajinn
Copy link

Wajinn commented Oct 31, 2024

As a user and upon reading this, have various thoughts about it.

1) Thought the point of keeping global variables was low level and compatibility issues with other languages.

If that is the case, then wouldn't various V users want them to be "accessed from anywhere in the code"? For the turned on global variables to act and be similar to the situation that people are use to in other low level languages... Not quite sure how module variables would be received, when V's __global is as easily identifiable as const, even for newbies.

2) My understanding is that team V wants to discourage both global and static variable usage.

Some time ago (upon me starting to play with V), I posted this discussion, #13926. The sentiment was for functions to use structs at the module level. Static variables only within functions and inside unsafe.

However, it can be argued that the documentation fails to make this usage clear, with examples of structs replacing usage of global and static variables. This might be leading to confusion or even frustration, because it's a different (even if arguably better) way. I do want to make it clear that I've become accustomed to V's way, and agree with the reasoning, but I do think team V may want to be mindful of the difference for people coming in from other languages.

@StunxFS
Copy link
Contributor Author

StunxFS commented Nov 1, 2024

Module variables could be used to handle C global variables perfectly, and this could also fix issue #22691, making it so that a module variable can be identified as external and thus used correctly in the code.

module main

@[c_extern('gVar')]
static mut c_var int

fn main() {
    c_var += 1
    println(c_var)
}

Of course, module variables could also be shared safely between threads, as long as the variable is not mutable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

4 participants