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

Discourage default arg for extension receiver #22492

Merged
merged 5 commits into from
Feb 20, 2025

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Feb 1, 2025

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.)

@som-snytt som-snytt force-pushed the issue/12460-extension-checks branch from 6037394 to cef2f79 Compare February 1, 2025 14:44
@som-snytt som-snytt marked this pull request as ready for review February 1, 2025 20:31
@som-snytt
Copy link
Contributor Author

Minor edit overlap with #22502

Comment on lines 1191 to 1194
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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also leverage HasDefaultParams

Comment on lines 2523 to 2524
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."""
Copy link
Member

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))
Copy link
Contributor

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

Copy link
Contributor Author

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.

@Sporarum
Copy link
Contributor

Sporarum commented Feb 18, 2025

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"

@som-snytt som-snytt force-pushed the issue/12460-extension-checks branch from b415f5e to 7539969 Compare February 18, 2025 19:24
@som-snytt
Copy link
Contributor Author

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 NoSymbol.debugName but maybe I don't and need to debugName() without having to pass in a sentinel value.

I added the sentence from the "nullification" message, which may also be dubious advice:

  | An extension method can be invoked as a regular method, but if that is the intended usage,
  | it should not be defined as an extension.

I previously saw a comment about evaluation order with by-name param, but it's not stranger than LazyList.

extension (x: => Any) def hello = "hello"

@main def test = println:
  def x = println("ugh")
  def y: LazyList[Unit] = x #:: y
  (x.hello, y) // (hello,LazyList(<not computed>)) but no ugh

but I agree it should be possible to write extensions without getting tricky.

Copy link
Member

@KacperFKorban KacperFKorban left a 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,
Copy link
Member

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,

Copy link
Contributor Author

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.

@som-snytt som-snytt force-pushed the issue/12460-extension-checks branch 2 times, most recently from 4ca1bd3 to e61b43a Compare February 19, 2025 21:03
@som-snytt som-snytt force-pushed the issue/12460-extension-checks branch from e61b43a to 3816ded Compare February 19, 2025 21:03
@KacperFKorban KacperFKorban enabled auto-merge (squash) February 20, 2025 14:14
@KacperFKorban KacperFKorban merged commit afb4806 into scala:main Feb 20, 2025
29 checks passed
@som-snytt som-snytt deleted the issue/12460-extension-checks branch February 20, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

forbid default values for extension parameters
3 participants