-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Syntax change for type parameters of extension methods #7455
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ given stringOps: { | |
} | ||
|
||
given { | ||
def (xs: List[T]) second[T] = xs.tail.head | ||
def [T](xs: List[T]) second = xs.tail.head | ||
} | ||
``` | ||
If such given instances are anonymous (as in the second clause), their name is synthesized from the name of the first defined extension method. | ||
|
@@ -106,8 +106,8 @@ as well as any type parameters of these extension methods into the given instanc | |
For instance, here is a given instance with two extension methods. | ||
```scala | ||
given listOps: { | ||
def (xs: List[T]) second[T]: T = xs.tail.head | ||
def (xs: List[T]) third[T]: T = xs.tail.tail.head | ||
def [T](xs: List[T]) second: T = xs.tail.head | ||
def [T](xs: List[T]) third: T = xs.tail.tail.head | ||
} | ||
``` | ||
The repetition in the parameters can be avoided by hoisting the parameters up into the given instance itself. The following version is a shorthand for the code above. | ||
|
@@ -151,13 +151,13 @@ to the implementation of right binding operators as normal methods. | |
The `StringSeqOps` examples extended a specific instance of a generic type. It is also possible to extend a generic type by adding type parameters to an extension method. Examples: | ||
|
||
```scala | ||
def (xs: List[T]) second [T] = | ||
def [T](xs: List[T]) second = | ||
xs.tail.head | ||
|
||
def (xs: List[List[T]]) flattened [T] = | ||
def [T](xs: List[List[T]]) flattened = | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
This need to be updated. Also I would add a paragraph about the position of explicit type parameters at call site. |
||
|
@@ -170,7 +170,7 @@ The required syntax extension just adds one clause for extension methods relativ | |
to the [current syntax](../../internals/syntax.md). | ||
``` | ||
DefSig ::= ... | ||
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses | ||
| ExtParamClause [nl] id DefParamClauses | ||
GivenDef ::= ... | ||
[GivenSig ‘:’] [ExtParamClause] TemplateBody | ||
ExtParamClause ::= [DefTypeParamClause] ‘(’ DefParam ‘)’ {GivenParamClause} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
object PostConditions { | ||
object PostConditions with | ||
opaque type WrappedResult[T] = T | ||
|
||
def result[T](given r: WrappedResult[T]): T = r | ||
|
||
def (x: T) ensuring [T](condition: (given WrappedResult[T]) => Boolean): T = { | ||
def [T](x: T) ensuring (condition: (given WrappedResult[T]) => Boolean): T = | ||
given WrappedResult[T] = x | ||
assert(condition) | ||
x | ||
} | ||
} | ||
|
||
object Test { | ||
object Test with | ||
import PostConditions.{ensuring, result} | ||
val s = List(1, 2, 3).sum.ensuring(result == 6) | ||
} |
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