Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Dimi Racordon <[email protected]>
  • Loading branch information
odersky and kyouko-taiga authored Oct 4, 2024
1 parent c76dc80 commit df75afc
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 20 deletions.
6 changes: 3 additions & 3 deletions docs/_docs/reference/contextual/context-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ The syntax of function types and function literals
is generalized as follows to allow context bounds for generic type parameters.

```ebnf
FunType ::= FunTypeArgs (‘=>’ | ?=>) Type
FunType ::= FunTypeArgs ('=>' | '?=>') Type
| DefTypeParamClause '=>' Type
FunExpr ::= FunParams (‘=>’ | ?=>) Expr
| DefTypeParamClause ‘=>’ Expr
FunExpr ::= FunParams ('=>' | '?=>') Expr
| DefTypeParamClause '=>' Expr
```
The syntax for abstract type members is generalized as follows to allow context bounds:

Expand Down
4 changes: 2 additions & 2 deletions docs/_docs/reference/contextual/deferred-givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SortedString[A] extends Sorted:
override given Ord[String] = ...
```

Note that the implementing given needs an `override` modifier since the `deferred` given in class `Sorted` counts as a concrete (i.e. not abstract) definition. In a sense, the `deferred` right hand side in `Sorted` is like a (magic, compiler-supported) macro, with the peculiarity that the macro's implementation also affects subclasses.
Note that the implementing given needs an `override` modifier since the `deferred` given in class `Sorted` counts as a concrete (i.e. not abstract) definition. In a sense, `deferred` on the right-hand side in `Sorted` is like a (magic, compiler-supported) macro, with the peculiarity that the macro's implementation also affects subclasses.

## Abstract Givens

Expand All @@ -52,6 +52,6 @@ trait HasOrd[T]:
```
An abstract given has the form `given name: Type` without a right-hand side or arguments to the type.

Since Scala 3.6, abstract givens are made redundant by deferred givens. Deferred givens can replace abstract givens. They have better ergonomics, since deferred givens get naturally implemented in inheriting classes, so there is no longer any need for boilerplate to fill in definitions of abstract givens.
Since Scala 3.6, abstract givens are made redundant by deferred givens. Deferred givens have better ergonomics, since they get naturally implemented in inheriting classes, so there is no longer any need for boilerplate to fill in definitions of abstract givens.

It is therefore recommended that software architectures relying on abstract givens be migrated to use deferred givens instead. Abstract givens are still supported in Scala 3.6, but will likely be deprecated and phased out over time.
6 changes: 3 additions & 3 deletions docs/_docs/reference/contextual/givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ given Position = enclosingTree.position
## Given Instance Initialization

An unconditional given instance without parameters is initialized on-demand, the first
time it is accessed. If the given is a simple alias to some immutable value, the given is implemented as a simple forwarder, without incurring the cost of a field to hold a cached value. If a given is conditional, a fresh instance is created for each reference.
time it is accessed. If the given is a mere alias to some immutable value, the given is implemented as a simple forwarder, without incurring the cost of a field to hold a cached value. If a given is conditional, a fresh instance is created for each reference.

## Syntax

Here is the full syntax for given instances. Some of these forms of givens are explained on a separate page on [Other Forms of Givens](../more-givens.md).
Here is the full syntax for given instances. Some of these forms of givens are explained in a separate page: [Other Forms of Givens](../more-givens.md).

```ebnf
Here is the complete context-free syntax for all proposed features.
Expand All @@ -106,7 +106,7 @@ GivenConditional ::= DefTypeParamClause
GivenType ::= AnnotType1 {id [nl] AnnotType1}
```
A given instance starts with the reserved word `given`, which is followed by
A given instance starts with the reserved keyword `given`, which is followed by
- An optional name and a colon
- An optional list of conditions.
Expand Down
6 changes: 2 additions & 4 deletions docs/_docs/reference/contextual/more-givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ given listOrd: [T] => (ord: Ord[T]) => Ord[List[T]]:

## By Name Givens

We sometimes find it necessary that a given alias is re-evaluated each time it is called. For instance, say we have a mutable variable `curCtx` and we want to define a given that returns the current value of that variable. A normal given alias will not do since by default given aliases are mapped to lazy vals.

In general, we want to avoid re-evaluation of givens. But there are situations like the one above where we want to specify _by-name_ evaluation instead. This is achieved by writing a conditional given with an empty parameter list:
Though in general we want to avoid re-evaluating a given, there are situations where such a re-evaluation may be necessary. For instance, say we have a mutable variable `curCtx` and we want to define a given that returns the current value of that variable. A normal given alias will not do since by default given aliases are mapped to lazy vals. In this case, we can specify a _by-name_ evaluation insteadby writing a conditional given with an empty parameter list:
```scala
val curCtx: Context
given context: () => Context = curCtx
```
With this definition, each time a `Context` is summoned we evaluate `context` function, which produces the current value of `curCtx`.
With this definition, each time a `Context` is summoned we evaluate the `context` function, which produces the current value of `curCtx`.

## Given Macros

Expand Down
7 changes: 3 additions & 4 deletions docs/_docs/reference/contextual/previous-givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ Note that the inline methods within the given instances may be `transparent`.
The inlining of given instances will not inline/duplicate the implementation of the given, it will just inline the instantiation of that instance.
This is used to help dead code elimination of the given instances that are not used after inlining.


## Pattern-Bound Given Instances

Given instances can also appear in patterns. Example:
Expand Down Expand Up @@ -215,12 +214,12 @@ Here is the syntax for given instances:

```ebnf
TmplDef ::= ...
| given GivenDef
| 'given' GivenDef
GivenDef ::= [GivenSig] StructuralInstance
| [GivenSig] AnnotType ‘=’ Expr
| [GivenSig] AnnotType
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’
StructuralInstance ::= ConstrApp {with ConstrApp} [‘with’ TemplateBody]
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ':'
StructuralInstance ::= ConstrApp {'with' ConstrApp} [‘with’ TemplateBody]
```

A given instance starts with the reserved word `given` and an optional _signature_. The signature
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/reference/experimental/typeclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ a bit cumbersome and limiting for standard generic programming patterns. Much ha
This note shows that with some fairly small and reasonable tweaks to Scala's syntax and typing rules we can obtain a much better scheme for working with type classes, or do generic programming in general.

The bulk of the suggested improvements has been implemented and is available
in under source version `future` if the additional experimental language import `modularity` is present. For instance, using the following command:
in source version `future` if the additional experimental language import `modularity` is present. For instance, using the following command:

```
scala compile -source:future -language:experimental.modularity
Expand Down
6 changes: 3 additions & 3 deletions docs/_docs/reference/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ ParamValueType ::= Type [‘*’]
TypeArgs ::= ‘[’ Types ‘]’
Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>>
TypeBounds ::= [‘>:’ Type] [‘<:’ Type]
TypeAndCtxBounds ::= TypeBounds [‘:’ ContextBounds]
TypeAndCtxBounds ::= TypeBounds [':' ContextBounds]
ContextBounds ::= ContextBound
| ContextBound `:` ContextBounds -- to be deprecated
| ContextBound ':' ContextBounds -- to be deprecated
| '{' ContextBound {',' ContextBound} '}'
ContextBound ::= Type ['as' id]
Types ::= Type {‘,’ Type}
Expand Down Expand Up @@ -441,7 +441,7 @@ TypeDef ::= id [HkTypeParamClause] {FunParamClause}TypeBounds
TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
| [‘case’] ‘object’ ObjectDef
| ‘enum’ EnumDef
| given (GivenDef | OldGivenDef)
| 'given' (GivenDef | OldGivenDef)
ClassDef ::= id ClassConstr [Template]
ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
ConstrMods ::= {Annotation} [AccessModifier]
Expand Down

0 comments on commit df75afc

Please sign in to comment.