Why V doesn't have "modular variables"? #24709
-
That is, you can declare mutable global variables: __global(
newline = false
tabs = 0
) But why can't we declare "modular variables"? Variables accessible only at module scope: module my_custom_module
// These variables are accessible only at module scope
mut newline := false
mut tabs := 0 Instead, we are forced to follow an approach similar to object-oriented programming. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 21 replies
-
This is the origin of my frustration: struct MyStruct {
my_var int
}
// the `aaa` method does not mutate `my_var`
fn (m &MyStruct) aaa() {
println(m.my_var)
}
fn (m &MyStruct) bbb() {
m.aaa()
}
fn (m &MyStruct) ccc() {
m.aaa()
} Both the struct MyStruct {
mut:
my_var int
}
// the `aaa` method mutates `my_var`
fn (mut m MyStruct) aaa() {
m.my_var++
}
fn (mut m MyStruct) bbb() {
m.aaa()
}
fn (mut m MyStruct) ccc() {
m.aaa()
} That is, a simple change in strategy forces me to modify a lot of code that is unrelated to that decision. |
Beta Was this translation helpful? Give feedback.
-
According to Claude "opinion" :) V sanctions global variables for the classic reasons that have dominated language design for decades, but which ironically contradict your initial point about modular simplicity. The "official" reasons why V requires -enable-globals: V doesn't allow global variables by default, but recognizes that "in low-level applications they have their place" and allows enabling them with the
But here's the irony: V forces you to use structs and methods (with all the complexity you just described), but then makes it difficult to use the simple modular approach you propose. It's as if it artificially pushes you toward objects. The philosophical problem: V seems to be applying anti-global dogma without considering that modular variables (not really "global" but scoped to the module) are different. A variable that is only accessible within a specific module doesn't have the same problems as a truly global variable. Languages that do it better:
It seems V is being conservative to the point of contradicting his own philosophy of simplicity. Your frustration is totally valid—he's forcing you toward object complexity while penalizing the simple approach you'd prefer to use. |
Beta Was this translation helpful? Give feedback.
-
"modular variables" - your words - are just namespaced globals. They have all the same problems that full globals do, just slightly narrowed due to the namespacing. Yes, they are simpler to use, but oh, so much harder to debug, since they can be modified from anywhere in the code. |
Beta Was this translation helpful? Give feedback.
-
I had a similar problem, a method returned read-only elements from a matching list. Then I needed return writeable elements to write some fields so I would had to change all the callers adding pub fn (b Boxes) box_read(src u8, box_fn fn(box Box)!) ! {
for _, box in b.boxes {
if box.on && box.id == src {
box_fn(box) !
}
}
}
pub fn (mut b Boxes) box_write(src u8, box_fn fn(mut box Box)!) ! {
for _, mut box in b.boxes {
if box.on && box.id == src {
box_fn(mut box) !
}
}
} I though was better to prevent read only callers from having mutable access and modifying any they should not and adding extra |
Beta Was this translation helpful? Give feedback.
-
Module variables, or "Module State"Upvoted. Recommended for V lang. Finding myself needing this, a lot. Module variables can greatly simplify programs in many cases.
Valid in C languageImportantly. Module state is already valid in C language. This will bring V code to parity with C lang. Example code
ConclusionModule variables can greatly simplify code. |
Beta Was this translation helpful? Give feedback.
-
The example you linked uses a *function-level* static var, not a module level var. How does a function-level static enable me to open a DB connection in a module's #init function, and use that DB connection later in another function in the same module? (Answer: it doesn't.)
…--
Earnest
***@***.***)
On Thu, 7 Aug 2025, at 15:25, Jorge Mireles wrote:
> the inability to store module state
>
I think V is *able* to store module states, and the state is *writable* and *readable* with a *single* public module function, see here for module `main`: https://play.vlang.io/p/2c8d313a6f . Works for any other modules.
—
Reply to this email directly, view it on GitHub <#24709 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BVV5KDFB2RMTGXKW2N53L2T3MNVWXAVCNFSM6AAAAAB7GRB252VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTIMBTGYZDSOA>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I haven't check how init and cleanup would interact with such function.
No offense but it sounds like you're not particularly qualified to comment on the matter.
…--
Earnest
***@***.***)
On Thu, 7 Aug 2025, at 17:50, Jorge Mireles wrote:
Yes, a function is needed and if were not public only module code could access the state only. But V is not C and the function hides the static object. Also I haven't check how `init` and `cleanup` would interact with such function. IMHO objects are better to manage shared objects that sooner or later need synchronization for web apps. Ultimately V calls C and there you could have your statics.
—
Reply to this email directly, view it on GitHub <#24709 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/BVV5KDAO6MI4ZHGNGAN26R33MOGXHAVCNFSM6AAAAAB7GRB252VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTIMBTG44DEMY>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I'm in agreement with JalonSolov on this. Do not think modular variables are needed and struct fields can be used instead. Many people are used to just throwing in global variables as a matter of habit. Part of the confusion appears to come from not clarifying and showing examples of what to use instead of global variables in V's documentation. Team V needs to be more aware of the habit, and arguably should take a few extras steps to explain and give examples of what to do instead. |
Beta Was this translation helpful? Give feedback.
"modular variables" - your words - are just namespaced globals.
They have all the same problems that full globals do, just slightly narrowed due to the namespacing.
Yes, they are simpler to use, but oh, so much harder to debug, since they can be modified from anywhere in the code.