Skip to content

Change syntax to instance-of-given. #6388

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

Merged
merged 1 commit into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’
ConstrApps ::= ConstrApp {‘with’ ConstrApp}
| ConstrApp {‘,’ ConstrApp}
ConstrApp ::= AnnotType {ArgumentExprs} Apply(tp, args)
| ‘(’ ConstrApp {‘given’ (InfixExpr | ParArgumentExprs)} ‘)’
ConstrExpr ::= SelfInvocation
| ConstrBlock
SelfInvocation ::= ‘this’ ArgumentExprs {ArgumentExprs}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/contextual-instance/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ calling the `error` method defined in `scala.compiletime`.
**Example:** Here is a slightly polished and compacted version of the code that's generated by inline expansion for the derived `Eql` instance of class `Tree`.

```scala
instance [T] given (elemEq: Eql[T]) of Eql[Tree[T]] {
instance [T] of Eql[Tree[T]] given (elemEq: Eql[T]) {
def eql(x: Tree[T], y: Tree[T]): Boolean = {
val ev = the[Generic[Tree[T]]]
val mx = ev.reflect(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait Codec[T] {

instance intCodec of Codec[Int] = ???

instance optionCodec[T] given (ev: => Codec[T]) of Codec[Option[T]] {
instance optionCodec[T] of Codec[Option[T]] given (ev: => Codec[T]) {
def write(xo: Option[T]) = xo match {
case Some(x) => ev.write(x)
case None =>
Expand Down
19 changes: 10 additions & 9 deletions docs/docs/reference/contextual-instance/instance-defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ instance IntOrd of Ord[Int] {
if (x < y) -1 else if (x > y) +1 else 0
}

instance ListOrd[T] given (ord: Ord[T]) of Ord[List[T]] {
instance ListOrd[T] of Ord[List[T]] given (ord: Ord[T]) {
def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match {
case (Nil, Nil) => 0
case (Nil, _) => -1
Expand All @@ -42,9 +42,9 @@ The name of an implicit instance can be left out. So the instance definitions
of the last section can also be expressed like this:
```scala
instance of Ord[Int] { ... }
instance [T] given (ord: Ord[T]) of Ord[List[T]] { ... }
instance [T] of Ord[List[T]] given Ord[T] { ... }
```
If a name is not given, the compiler will synthesize one from the type(s) in the `for` clause.
If a name is not given, the compiler will synthesize one from the type(s) in the `of` clause.

## Alias Instances

Expand All @@ -67,12 +67,13 @@ Here is the new syntax of instance definitions, seen as a delta from the [standa
```
TmplDef ::= ...
| ‘instance’ InstanceDef
InstanceDef ::= [id] InstanceParams InstanceBody
InstanceParams ::= [DefTypeParamClause] {GivenParamClause}
InstanceDef ::= [id] [DefTypeParamClause] InstanceBody
InstanceBody ::= [‘of’ ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
| ‘of’ Type {GivenParamClause} ‘=’ Expr
ConstrApp ::= AnnotType {ArgumentExprs}
| ‘(’ ConstrApp {‘given’ (InfixExpr | ParArgumentExprs)} ‘)’
GivenParamClause ::= ‘given’ (‘(’ [DefParams] ‘)’ | GivenTypes)
InstanceBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
| ‘for’ Type ‘=’ Expr
GivenTypes ::= AnnotType {‘,’ AnnotType}
```
The identifier `id` can be omitted only if either the `for` part or the template body is present.
If the `for` part is missing, the template body must define at least one extension method.
The identifier `id` can be omitted only if either the `of` part or the template body is present.
If the `of` part is missing, the template body must define at least one extension method.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Box[T](x: T) derives Eql
By the usual rules if [typeclass derivation](./derivation.html),
this generates the following `Eql` instance in the companion object of `Box`:
```scala
instance [T, U] given Eql[T, U] of Eql[Box[T], Box[U]] = Eql.derived
instance [T, U] of Eql[Box[T], Box[U]] given Eql[T, U] = Eql.derived
```
That is, two boxes are comparable with `==` or `!=` if their elements are. Examples:
```scala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Instance definitions can be mapped to combinations of implicit objects, classes
```
2. Parameterized instance definitions are mapped to combinations of classes and implicit methods. E.g.,
```scala
instance ListOrd[T] given (ord: Ord[T]) of Ord[List[T]] { ... }
instance ListOrd[T] of Ord[List[T]] given (ord: Ord[T]) { ... }
```
maps to
```scala
Expand Down