-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Scala Wart: Callers of zero-parameter methods can decide how many parens to use #2571
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
Comments
I've expanded a lot more elsewhere in opposition to this change (https://news.ycombinator.com/item?id=14431208), but: If both:
Then I think that this behaviour makes sense 👍, if only because it makes the behaviour more consistent (where no parens -> I think that this behaviour in combination with the automatic expansion of no-parens methods in #2570 makes for really confusing behaviour, as I've expanded upon in that above news.yc link. |
From the perspective of the uniform access principle, it makes sense that a caller can use Would it make sense to view this as similar to an implicit conversion |
@tekacs yeah I've tweaked the text since the original blog post to better incorporate the discussion I had with people and clarify points I think were confusing. Thanks for all your feedback on the original post! |
Scala 1 (2004-2005) had the precise behavior advocated by this issue, but here is what killed it:
Same for toString, and any number of other commonly used methods. That's simply unacceptable behavior. It was unacceptable then and is unacceptable now. It means the uniform access principle would not work at all for anything we get from Java. It would stop being a principle then. For a short while, Scala then had the rule that The reason for dropping this rule at the time was that with it, converting libraries from Java to Scala would risk breaking client code. Let's say you have int length() in a Java library. Clients can call this with
and not def length(): Int Clients that used
on an
because there's a side effect. So clients using it as
which often only have a "read" effect. The uniform access principle allows such methods to be parameterless, but some writers might prefer the parens to emphasize the read effect. So we are on more shaky ground here. If |
👍 to disallowing calling nullary methods without parens. To build on what @odersky said in the previous post. I believe it would be a good compromise to allow calling Java-defined methods without the AFAIK Kotlin does similar things for non-nullable types using annotations. Which allows them to write Kotlin-friendly libraries in Java. |
How often are you using the entire standard library from Java? Stop writing Java in Scala, Guillaume! 😂 I would not mind writing parens when calling Java methods. But sure, I see your point. Perhaps I'm being too harsh 👍 Martin's proposal is more pragmatic for sure. |
I'd be happy for the "restricted to Java APIs, not Scala APIs (unless you add a special annotation)" solution. Having the behavior accessible via an annotation would also solve this problem:
Apart from allowing us to migrate libraries without breaking client code, it's probably also a good idea just to have all the various semantics expressible in the Scala language, rather than having special behavior that is only expressible by writing Java source code. An annotation that can allow Scala methods to achieve the same call-site magic would allow that |
I'm in favor of this change. (This has been discussed at least twice before, at scala/bug#4506 and a previous discussion I dimly recall but can't find (Paul on 4506: "I think we should either be stricter at the call sites (and there was some momentum for that a while ago, but I think martin decided against)"). But, I think the commenters on this ticket have done a really good job of making the relevant points again, so I don't think anybody else needs to go digging.) |
Fixed by #2716 |
Uh oh!
There was an error while loading. Please reload this page.
Opening this issue, as suggested by Martin, to provide a place to discuss the individual warts brought up in the blog post Warts of the Scala Programming Language and the possibility of mitigating/fixing them in Dotty (and perhaps later in Scala 2.x). These are based on Scala 2.x behavior, which I understand Dotty follows closely, apologies in advance if it has already been fixed
Scala lets you leave off empty-parens lists when calling functions. This
looks kind of cute when calling getters:
However, it doesn't really make sense when you consider how this works in
most other languages, such as Python:
After all, if
getFoo()
is aInt
, why shouldn'tgetFoo
without theparens be a
() => Int
? After all, calling a() => Int
with parensgive you an
Int
. However, in Scala methods are "special", as shown above,and methods with empty parens lists are treated even more specially.
Furthermore, this feature really doesn't make sense when you start pushing it:
Is this really the behavior we expect in a statically-typed language, that you
can call this method with any number of argument lists
0 < n <= 5
andit'll do the same thing regardless? What on earth is the type of
bar
? TheScala community likes to think that it's "definition-side variance" is better
than Java's "use-site variance", but here we have Scala providing
definition-site parens where every caller of
bar
can pick and choose how manyparens they want to pass.
I think the solution to this is clear: methods should be called with as many
sets of parentheses as they are defined with (excluding implicits). Any
method call missing parens should be eta-expanded into the appropriate
function value.
Concretely, that means that given these two functions:
They currently behave like this:
And will there-after behave like this:
Notably, this does not take away the ability to control how many empty-parens
a function is called with; rather, it shifts that decision from the user of a
function to the author of a function. Since the author of a function already
decides everything else about it (It's name, arguments, return type,
implementation, ...) giving the author the decision over empty-parens would
not be unprecedented.
No-parens "property" functions would still be possible, the author of the
function would just need to define it without parens, as is already
possible:
The only reason I've heard for this feature is to "let you call Java
getFoo
methods without the parens", which seems like an exceedingly weak justification
for a language feature that so thoroughly breaks the expectations of a
statically-typed language. If that was the problem, one option would be to
allow use-site optional empty-parentheses only at Java call-sites or Scala
call-sites with a particular annotation (
@optionalParens def foo = ...
?).This would limit the scope of this behavior to a mild Java-interop quirk
(one of many), rather than a wart affecting the core of the Scala programming
language
PostScript:
One reason in support of the current behavior I have encountered repeatedly in online forums is that this allows you to "fix" library APIs so they better match the pure/non-pure semantics of their functions: so if a library author writes a pure function with parentheses, you can "fix" the API and call it without parentheses. That way your code looks "idiomatic" and "correct" w.r.t. purity and parens even if upstream authors mess up.
I don't really think this is a good justification, for the exact same logic can be used to justify unlimited monkey-patching of upstream code, which I think most will agree does not make sense. Reductio ad absurdum
The text was updated successfully, but these errors were encountered: