-
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
Syntax change for type parameters of extension methods #7455
Conversation
The new syntax has the type parameters come first. I.e. ``` def [T](xs: List[T]) append (ys: List[T]): List[T] = ... ``` instead of ``` def (xs: List[T]) append [T] (ys: List[T]): List[T] = ... ``` Application is unchanged, so it is still ``` xs.append[String](ys) ``` An argument for the old syntax is that it aligns definition and call syntax. On the other hand, the new syntax maintains the general rule that parameter introductions always com before parameter uses. The decisive argument to switch is to be consistent with the new collective parameter syntax, where `append` would be written like this: ``` given [T](xs: List[T]) def append (ys: List[T]): List[T] = ... ``` To avoid misalignment of type parameters between definition and call syntax, we considered disallowing explicit type parameters for extension methods altogether, and to require that the method is called as a normal method instead. But that would not work for anonymous givens as in the last exaple above.
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 would add a neg test:
// dotc -Xfatal-warnings
def (self: T) foo[T] = ??? // error
def [T1](self: T1) bar[T2] = ??? // error
if (in.token == LPAREN) | ||
try (paramClause(0, prefix = true) :: Nil, Method | Extension) | ||
finally newLineOpt() | ||
def extParamss = try paramClause(0, prefix = true) :: Nil finally newLineOpt() |
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: def extParamss()
since the function is effectful
xs.foldLeft[List[T]](Nil)(_ ++ _) | ||
|
||
def (x: T) + [T : Numeric](y: T): T = | ||
def [T: Numeric](x: T) + (y: T): T = | ||
summon[Numeric[T]].plus(x, y) | ||
``` | ||
|
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.
As usual, type parameters of the extension method follow the defined method name. Nevertheless, such type parameters can already be used in the preceding parameter clause.
This need to be updated. Also I would add a paragraph about the position of explicit type parameters at call site.
Aha, another weird special rule which breaks again the consistency of the language. another funny thing is that to support |
The new syntax has the type parameters come first. I.e.
instead of
Application is unchanged, so it is still
An argument for the old syntax is that it aligns definition and call syntax.
On the other hand, the new syntax maintains the general rule that parameter introductions
always com before parameter uses. The decisive argument to switch is to be consistent
with the new collective parameter syntax, where
append
would be written like this:To avoid misalignment of type parameters between definition and call syntax, we considered
disallowing explicit type parameters for extension methods altogether, and to require
that the method is called as a normal method instead. But that would not work for anonymous
givens as in the last exaple above.