From 94562bf6abe3b12664f8e61b1adc9599c99fff33 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 27 Apr 2019 11:23:40 +0200 Subject: [PATCH] Change syntax to instance-of-given. Also, handle superclass arguments with given arguments. --- docs/docs/internals/syntax.md | 1 + .../contextual-instance/derivation.md | 2 +- .../inferable-by-name-parameters.md | 2 +- .../contextual-instance/instance-defs.md | 19 ++++++++++--------- .../multiversal-equality.md | 2 +- .../relationship-implicits.md | 2 +- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/docs/docs/internals/syntax.md b/docs/docs/internals/syntax.md index a9b3a28ecf17..97a1fd161eb5 100644 --- a/docs/docs/internals/syntax.md +++ b/docs/docs/internals/syntax.md @@ -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} diff --git a/docs/docs/reference/contextual-instance/derivation.md b/docs/docs/reference/contextual-instance/derivation.md index 72c12b951bb1..d093893aecbb 100644 --- a/docs/docs/reference/contextual-instance/derivation.md +++ b/docs/docs/reference/contextual-instance/derivation.md @@ -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) diff --git a/docs/docs/reference/contextual-instance/inferable-by-name-parameters.md b/docs/docs/reference/contextual-instance/inferable-by-name-parameters.md index 651591d5edf1..52bb75d3af9e 100644 --- a/docs/docs/reference/contextual-instance/inferable-by-name-parameters.md +++ b/docs/docs/reference/contextual-instance/inferable-by-name-parameters.md @@ -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 => diff --git a/docs/docs/reference/contextual-instance/instance-defs.md b/docs/docs/reference/contextual-instance/instance-defs.md index cc40a8354b65..044570e412d6 100644 --- a/docs/docs/reference/contextual-instance/instance-defs.md +++ b/docs/docs/reference/contextual-instance/instance-defs.md @@ -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 @@ -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 @@ -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. diff --git a/docs/docs/reference/contextual-instance/multiversal-equality.md b/docs/docs/reference/contextual-instance/multiversal-equality.md index d9f0fce13560..9e8526b9a1aa 100644 --- a/docs/docs/reference/contextual-instance/multiversal-equality.md +++ b/docs/docs/reference/contextual-instance/multiversal-equality.md @@ -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 diff --git a/docs/docs/reference/contextual-instance/relationship-implicits.md b/docs/docs/reference/contextual-instance/relationship-implicits.md index 61af35456ffa..945647672a67 100644 --- a/docs/docs/reference/contextual-instance/relationship-implicits.md +++ b/docs/docs/reference/contextual-instance/relationship-implicits.md @@ -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