-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Discourage default arg for extension receiver #22492
Discourage default arg for extension receiver #22492
Conversation
6037394
to
cef2f79
Compare
Minor edit overlap with #22502 |
val receiverName = sym.info.explicit.firstParamNames.head | ||
val num = sym.info.paramNamess.flatten.indexWhere(_ == receiverName) | ||
val getterName = DefaultGetterName(sym.name.toTermName, num = num) | ||
val getterDenot = sym.owner.info.member(getterName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you move that into a local function? The logic might be slightly clearer then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also leverage HasDefaultParams
i"""Although extensions are ordinary methods, they must be invoked as a selection. | ||
|Therefore, the receiver cannot be omitted. A default argument for that parameter would never be used.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: I think that this message might be confusing, since it's technically false. They don't have to be invoked as a selection.
I can agree on adding a warning here, since it in most cases it's undesired to have a default value for a receiver of an extension method, but I we should make the message conditional -- e.g. When invoking an extension method as a selection, the receiver cannot be omitted.
} | ||
mapParamss(takeUpTo(paramssNoRHS, n)) | ||
(tparam => dropContextBounds(toMethParam(tparam, KeepAnnotations.All))) | ||
(vparam => toMethParam(vparam, KeepAnnotations.All, keepDefault = false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change makes the code harder to read, and harder to expand later
In the case of methods with multiple lambda parameters, it is advised to stick to braced code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes it clear that there are two arg lists consisting of a simple lambdas.
What looks new is that we know the expression continues. This is a feature.
It's an anti-feature to expect to expand the code in these blocks, which would turn them from merely ugly to inscrutable.
Alternatively
def getterParamss(n: Int): List[ParamClause] =
inline def m = mapParamss(takeUpTo(paramssNoRHS, n)):
tparam => dropContextBounds(toMethParam(tparam, KeepAnnotations.All))
m:
vparam => toMethParam(vparam, KeepAnnotations.All, keepDefault = false)
I don't agree with your premise, but I'll revert it on the basis that it is not my code.
I was going to ask about the following case: extension (x: => Any)
def hello = "hello"
{println("print me")}.hello But it seems this would not necessarily be a problem: class Foo(y: Int):
inline def hello = "hello"
lazy val x =
println("x")
Foo(1)
x.hello // returns "hello" without printing "x" Edit: Or maybe yes ? class Foo(y: Int):
inline def hello = "hello"
{println("x"); Foo(1)}.hello // returns "hello" AND prints "x" |
b415f5e
to
7539969
Compare
I think the warning is a bit dubious, but maybe the question is: What were they trying to accomplish? I might have a "zero" value to invoke an extension, such as I added the sentence from the "nullification" message, which may also be dubious advice:
I previously saw a comment about evaluation order with by-name param, but it's not stranger than
but I agree it should be possible to write extensions without getting tricky. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now, added a small comment about the warning message.
I think that the the by-name receiver isn't in the scope of this PR and #17321 should probably address that issue.
def msg(using Context) = | ||
i"""Extension method ${hl(method.name.toString)} should not have a default argument for its receiver.""" | ||
def explain(using Context) = | ||
i"""Although extensions are ordinary methods, when they are invoked as a selection, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think that the newline makes the message hard to parse now, since I first read only the first line, which is grammatically correct – Although extensions are ordinary methods, when they are invoked as a selection,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a complete sentence. However, I have rephrased to use no clauses.
4ca1bd3
to
e61b43a
Compare
e61b43a
to
3816ded
Compare
Fixes #12460
I shied away from making it an error, as that is a language change that violates the rule that extension methods are ordinary methods. There are other restrictions, but an extension always allows explicit invocation
m()(x)
that could leverage a default.The caret is wrong on the second test case (
todo). Edit: span of default getter was union of first parameter and the param RHS, so that the synthetic position of the getter symbol was a point at the first parameter. Now the getter tree gets the span of the RHS. (That span is non-synthetic, but the definition is still synthetic. The name pos of a synthetic is the point.)