Ideas about global variable, function, and method #1493
hihig2001
started this conversation in
Language design
Replies: 1 comment
-
Thanks for bringing this up! We're still figuring out what to do about global variables. I agree with you that, if they exist, tracking them explicitly is better than not tracking them at all. However, this only works for one level deep. What happens when someone calls a function that calls globalvar g: i32 = 5;
fn StrangeSum[g: global](x: i32, y: i32) -> i32 {
g = g + 1;
return x + y + g;
}
fn PublicSum(x: i32, y: i32) -> i32 {
return StrangeSum(x, y);
} Does |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've read documents about global variable, function, and method.
I thought for long time function and it's context aware.
I think problem of global variable is not it self, the problem is that context of function is implicit.
I always thought that member function in C++ has implicit contexts about member variables.
Also free function in C++ has implicit contexts about global variables.
On the other hand, lambda in C++ has explicit contexts by capture clause.
I imagine that method syntax in Carbon came from lambda syntax in C++ cause '[me: Self]' clause reminds me capture clause of lambda. And I think it it great separating contexts with parameters.
because contexts is by it means, something related contextually related with function.
I think contexts and parameter has different property in use case wise, which is why people don't stop using global variables.
My idea is that function syntax be something like
fn Sum[g: global](Int a, Int b) -> Int
by stating explicit way that free function has contexts of global variable, you can solve problems of global variable.
because programmer who wanna write function that is not access global variables simply opt out capture clause.
fn Sum(Int a, Int b) -> Int
like this.
if above has done context of global could be even granular.
fn Sum[global_var: global.var](Int a, Int b) -> Int
I think call site syntax should be no difference with global context or without.
Sum(3, 5)
but IDE might can help with 'inline hint' like this by showing context clause.
I think explicitness is good in general, it also gives good defaults in programming language.
Instead giving some kind of keyword to blocking access global variable, no global variable access by default.
I guess it may also helps if you're planning some kind of 'universal function call syntax' mechanism.
I'm not sure how would work in implementation side, which may results in code bloat? I don't know.
but these feature is something I always want to have in programming language.
Explicitly stating contexts of function.
Beta Was this translation helpful? Give feedback.
All reactions