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

No way to execute method with empty params when field with the same name is present #105

Open
uvlad7 opened this issue Apr 14, 2024 · 2 comments
Assignees
Labels

Comments

@uvlad7
Copy link

uvlad7 commented Apr 14, 2024

So, I have java class

public class Test {
    public String test;

    public Test(String test) {
        this.test = test;
    }

    public String test() {
        return "method_" + this.test;
    }
}

and I can't call test() even with _invoke

3.0.2 :011 > Test.new("test").test
 => "test" 
3.0.2 :012 > Test.new("test")._invoke(:test)
 => "test" 

because invoke_by_instance checks if getter should be called simply by checking if argc == 0 (if that field exists), I think it should have an additional bool parameter to skip getter if it's called from rjb_i_invoke.

@arton arton self-assigned this May 1, 2024
@arton arton added the javaish label May 1, 2024
@arton arton closed this as completed in 9f64bdf May 1, 2024
arton added a commit that referenced this issue May 1, 2024
fix #105 add extra parameter to invoke_by_instance
@arton
Copy link
Owner

arton commented May 1, 2024

@uvlad7 thank you for your desribed report.
I implemented it at Rjb 1.7.3.

@uvlad7
Copy link
Author

uvlad7 commented May 1, 2024

@arton, thank you for the fix.

But it just hit me that I missed some nuances.

First, I assume, _invoke was added to call methods with names that are not valid ruby names, but it could be used the same fuels, and now there's no way to access a field names end, for example. So, I assume something like _field (set if arity 2, get if 1) or _set_field/_get_field is required.

Second, is it really a good idea to silently resolve this ambiguity, maybe it's better to raise an exception and force programmer to use an explicit way?
Even if so, I believe it's more expected for rubyists to prefer a method over a field. I mean, we often replace

attr_reader :something

with

def something
  do_something_additional
  @something
end

and it works as public interface doesn't change, so one may expect things to work the same way if they add something() in Java. l didn't check if making something non-public helps, but we don't always can change the interface, anyway

@arton arton reopened this May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants