gravity
gravity copied to clipboard
Private Method isn't private
Hello there. I was writing some examples to test and document the access modifiers and I found that the code below works:
class TestAccessModifiers{
public var my_var = "I'm public!";
private var my_hidden_var = "I'm private!";
public func get_hidden(){
return my_hidden_var;
}
private func get_public(){
return my_var;
}
}
func puts(str){
System.print(str);
}
func main(){
var test = TestAccessModifiers();
test.my_var;
//puts("Public var holds = " + test.my_var );//works
//puts("Get hidden var from public method = " + test.get_hidden());//works
//puts("Private var holds = " + test.my_hidden_var );//works and cause error like it should
puts("Get public var from private method = " + test.get_public());//doesn't works
}
As result, from the terminal, I get :
Get public var from private method = I'm public!
I donno, but I guessing that the keyword private before the keyword func should print the same, or almost the same value, as in "puts("Private var holds = " + test.my_hidden_var );", which is :: RUNTIME ERROR: Unable to find my_hidden_var into class TestAccessModifiers
Thanks!
PS: I've used Gravity version 0.5.0.
Hi @felipetesc I should probably explain it better, but in the current implementation private means private from the outside and not from within the class itself.
Yes, I understood that previously, and I've used the function which the signature is private func get_public() from outside the class, inside the main func. Perhaps the previous example is confuse, so here it's another:
//file test test_am.gravity
class Test{
private func getVal(){
return "my string";
}
}
//main.gravity
#include " test_am.gravity"
func main(){
var test = Test();
var val = test.getVal();// get val is a private method
System.print(val);
}
When I execute gravity main.gravity I've got this result: my string RESULT: NULL (in 0.0338 ms)
@felipetesc seems like an issue that needs to be fixed. Thanks!