Skip to content
Merged
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a5c8aef
trying to rewrite typeclasses-new
Jan 6, 2020
1bd517e
Wrap the last sentence in a whole "summary" section to avoid stray text
Jan 31, 2020
171a09a
Step2: rewrite the Functor part in more details
Jan 31, 2020
6b7e497
Step3: rewrite the Monad part (apart from the Reader monad)
Jan 31, 2020
74ff540
Fix typos
Jan 31, 2020
8118ed7
Rephrase assertTransformation simplification
Jan 31, 2020
a076eea
Fix typos on the reader monad
Jan 31, 2020
66ebcfb
@bishabosha's note
Jan 31, 2020
334c4b4
attempt at explaining the reader monad
Jan 31, 2020
cd501b6
less concrete and more accurate definition of a Functor
aesteve Jan 31, 2020
ff7ca15
definition of A functor, not THE functor ability
aesteve Jan 31, 2020
cf29e82
A functor for the type constructor F[_]
aesteve Jan 31, 2020
8607014
better phrasing for abstracting away Config
aesteve Jan 31, 2020
4c81c46
parameterised type with abstract members => trait
aesteve Jan 31, 2020
6b6cd39
oo polymorphism vs. parametric polymorphism
aesteve Jan 31, 2020
c6ec581
explaining the difference between OO polymorphism and ad-hoc polymorp…
aesteve Jan 31, 2020
716ae0c
better phrasing for conclusion
aesteve Jan 31, 2020
c15cd63
remove the "we don't care" part
aesteve Jan 31, 2020
8b85b34
using F as a substitution for every type that ca be mapped over
aesteve Jan 31, 2020
86615f6
Merge remote-tracking branch 'upstream/master' into doc/rework-typecl…
Feb 9, 2020
f2d7a6d
Merge remote-tracking branch 'origin/doc/rework-typeclasses-new' into…
Feb 9, 2020
df15a24
Trying to add an easy-to-grasp definition of type classes
Feb 9, 2020
4b04603
Proper 0.23 syntax
Feb 10, 2020
cb6c511
Proper 0.23 syntax
Feb 10, 2020
cd9834e
typo
Feb 10, 2020
bf7b005
no longer `given as`
Feb 10, 2020
9c1509b
typeclasses-new.md is now typeclasses.md
Feb 13, 2020
466bff9
Adapt to 0.23 latest
Feb 26, 2020
219bc3c
Use new type wildcard syntax
Mar 30, 2020
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/docs/reference/contextual/typeclasses-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ assert(3 == List(1, 2).combineAll)
### Functors:

A `Functor` for a type provides the ability for its values to be "mapped over", i.e. apply a function that transforms inside a value while remembering its shape. For example, to modify every element of a collection without dropping or adding elements.
Let's name our "type containing zero or more elements" `F`. It's a type constructor: the type of its values becomes concrete when provided a type argument.
Therefore we'll write it `F[_]` since we don't really care about the type of the elements it contains.
The definition of the `Functor` ability would thus be written as:
We can represent all types that can be "mapped over" with `F`. It's a type constructor: the type of its values becomes concrete when provided a type argument.
Therefore we write it `F[_]`, hinting that it is a type with internal details we can inspect.
The definition of a generic `Functor` would thus be written as:

```scala
trait Functor[F[_]] {
def map[A, B](original: F[A], mapper: A => B): F[B]
}
```

Which could read as follows: "The `Functor` ability for a wrapper type `F` represents the ability to transform `F[A]` to `F[B]` through the application of the `mapper` function whose type is `A => B`".
Which could read as follows: "A `Functor` for the type constructor `F[_]` represents the ability to transform `F[A]` to `F[B]` through the application of the `mapper` function whose type is `A => B`". We call the `Functor` definition here a _typeclass_.
This way, we could define an instance of `Functor` for the `List` type:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This way, we could define an instance of `Functor` for the `List` type:
This way, we could define an instance of `Functor` for the `List` type constructor:

```scala
Expand Down Expand Up @@ -252,7 +252,7 @@ given configDependentMonad as Monad[[Result] =>> Config => Result]



The Reader monad allows to abstract over the `Config` type, named `Ctx` in the following examples. It is therefore _parameterized_ by `Ctx`:
It is likely that we would like to use this pattern with other kinds of environments than our `Config` trait. The Reader monad allows us to abstract away `Config` as a type _parameter_, named `Ctx` in the following definition:

```scala
given readerMonad[Ctx] as Monad[[X] =>> Ctx => X] {
Expand All @@ -265,8 +265,8 @@ given readerMonad[Ctx] as Monad[[X] =>> Ctx => X] {

### Summary

The definition of a _typeclass_ is expressed in Scala 3 via a `trait`.
The main difference with other traits resides in how these traits are implemented.
In the case of a _typeclass_ the trait's implementations are expressed through `given ... as` type definitions, and not through classes that `extends` the trait linearly.
The definition of a _typeclass_ is expressed via a parameterised type with abstract members, such as a `trait`.
The main difference between object oriented polymorphism, and ad-hoc polymorphism with _typeclasses_, is how the definition of the _typeclass_ is implemented, in relation to the type it acts upon.
In the case of a _typeclass_, its implementation for a concrete type is expressed through a `given ... as` term definition, which is supplied as an implicit argument alongside the value it acts upon. With object oriented polymorphism, the implementation is mixed into the parents of a class, and only a single term is required to perform a polymorphic operation.

In addition to these given instances, other constructs like extension methods, context bounds and type lambdas allow a concise and natural expression of _typeclasses_.
To conclude, in addition to given instances, other constructs like extension methods, context bounds and type lambdas allow a concise and natural expression of _typeclasses_.