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

Support named arguments when calling Java #5896

Closed
scabug opened this issue Jun 9, 2012 · 10 comments
Closed

Support named arguments when calling Java #5896

scabug opened this issue Jun 9, 2012 · 10 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jun 9, 2012

This seems needlessly uncooperative:

scala> new java.io.PrintWriter(new java.io.File("/tmp/foo"), csn = "UTF-8")
<console>:8: error: overloaded method constructor PrintWriter with alternatives:
  (java.io.File,String)java.io.PrintWriter <and>
  (java.io.File)java.io.PrintWriter <and>
  (String,String)java.io.PrintWriter <and>
  (String)java.io.PrintWriter <and>
  (java.io.OutputStream,Boolean)java.io.PrintWriter <and>
  (java.io.OutputStream)java.io.PrintWriter <and>
  (java.io.Writer,Boolean)java.io.PrintWriter <and>
  (java.io.Writer)java.io.PrintWriter
 cannot be applied to (java.io.File, csn: String)
              new java.io.PrintWriter(new java.io.File("/tmp/foo"), csn = "UTF-8")
              ^

scala> new java.io.PrintWriter(new java.io.File("/tmp/foo"), "UTF-8")
res1: java.io.PrintWriter = java.io.PrintWriter@31c24543

It would be great to do all the named/positional business in those situations where we can obtain the names of java method parameters; but failing that, at least it doesn't have to stop me from documenting the call in a situation where it would compile fine with the unlabeled arguments.

@scabug
Copy link
Author

scabug commented Jun 9, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5896?orig=1
Reporter: @paulp

@scabug
Copy link
Author

scabug commented Jun 11, 2012

@lrytz said (edited on Jun 11, 2012 7:22:00 AM UTC):
you mean to drop parameter names? i don't think that's a good idea. it's confusing that code with wrong names works. also remember that named arguments can be in any order, so just removing the names has surprising results.

i agree that we should have a better error message.

@scabug
Copy link
Author

scabug commented Jun 11, 2012

@paulp said:
That's true, working with wrong or ambiguous names is undesirable. This is a real bummer though. It dramatically improves the readability of calls into java, particularly for String and Boolean arguments. It would be great to at least open some way for motivated persons to take advantage of named arguments - I would gladly write a source transformer and run it over the whole source base, so wrong/ambiguous names would not be a big concern. The scala philosophy is ostensibly to not be so keen to protect me from myself that I can't do the useful thing I want to do.

@scabug
Copy link
Author

scabug commented Oct 21, 2012

@OlegYch said:
this may be significantly affected by http://openjdk.java.net/jeps/118

@scabug
Copy link
Author

scabug commented Oct 21, 2012

@paulp said:
Thanks for the link! I hadn't heard about that.

@scabug
Copy link
Author

scabug commented Oct 21, 2012

@paulp said:
Reopening as "suggested enhancement" so I don't lose track of it.

@SethTisue SethTisue changed the title Named arguments should at least be ignored when calling java Support named arguments when calling Java Mar 6, 2019
@SethTisue
Copy link
Member

I've renamed the ticket, given http://openjdk.java.net/jeps/118, and since I agree that ignoring isn't a good idea

@SethTisue SethTisue added this to the Backlog milestone Mar 6, 2019
@hrhino
Copy link
Member

hrhino commented Mar 6, 2019

Is this ticket for more than this?

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ cat J_1.java 
package x;

public class J_1 { public static void args(int x, String y) {} }

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ cat J_2.java 
package x;

public class J_2 { public static void args(int x, String y) {} }

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ cat S.scala 
package x

object S { J_1.args(x = 4, y = "foo"); J_2.args(x = 4, y = "foo") }

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ javac -d . -parameters J_1.java 

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ scalac -d . -cp . J_2.java S.scala 

[nix-shell:/run/user/1000/tmp.eRjomaIrtO]$ 

@SethTisue
Copy link
Member

oh, fixed by scala/scala#4735 in 2.12.0 👍

@SethTisue SethTisue modified the milestones: Backlog, 2.12.0 Mar 6, 2019
@SethTisue
Copy link
Member

sadly, as a comment on that PR says, "the Java standard library isn't compiled with -parameters" 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants