Skip to content

Commit 2131486

Browse files
authored
Merge pull request #1242 from fabiopakk/patch-2
Fixed parameters vs arguments use and wording regarding substitution
2 parents 85eb4ce + 827db84 commit 2131486

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

_tour/variances.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Fortunately, the compiler stops us long before we could get this far.
138138

139139
### Other Examples
140140

141-
Another example that can help one understand variance is `trait Function1[-T, +R]` from the Scala standard library. `Function1` represents a function with one argument, where the first type parameter `T` represents the argument type, and the second type parameter `R` represents the return type. A `Function1` is contravariant over its argument type, and covariant over its return type. For this example we'll use the literal notation `A => B` to represent a `Function1[A, B]`.
141+
Another example that can help one understand variance is `trait Function1[-T, +R]` from the Scala standard library. `Function1` represents a function with one parameter, where the first type parameter `T` represents the parameter type, and the second type parameter `R` represents the return type. A `Function1` is contravariant over its argument type, and covariant over its return type. For this example we'll use the literal notation `A => B` to represent a `Function1[A, B]`.
142142

143143
Assume the similar `Cat`, `Dog`, `Animal` inheritance tree used earlier, plus the following:
144144

@@ -147,7 +147,7 @@ abstract class SmallAnimal extends Animal
147147
case class Mouse(name: String) extends SmallAnimal
148148
```
149149

150-
Suppose we're working with functions that accept types of animals, and return the types of food they eat. If we would like a `Cat => SmallAnimal` (because cats eat small animals), but are given a `Animal => Mouse` instead, our program will still work. Intuitively an `Animal => Mouse` will still accept a `Cat` as an argument, because a `Cat` is an `Animal`, and it returns a `Mouse`, which is also a `SmallAnimal`. Since we can safely and invisibly substitute the former for the latter, we can say `Animal => Mouse` is a subtype of `Cat => SmallAnimal`.
150+
Suppose we're working with functions that accept types of animals, and return the types of food they eat. If we would like a `Cat => SmallAnimal` (because cats eat small animals), but are given a `Animal => Mouse` instead, our program will still work. Intuitively an `Animal => Mouse` will still accept a `Cat` as an argument, because a `Cat` is an `Animal`, and it returns a `Mouse`, which is also a `SmallAnimal`. Since we can safely and invisibly substitute the former by the latter, we can say `Animal => Mouse` is a subtype of `Cat => SmallAnimal`.
151151

152152
### Comparison With Other Languages
153153

0 commit comments

Comments
 (0)