Skip to content

reduced number of Logarithm #11677

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 2 commits into from
Mar 13, 2021
Merged
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
10 changes: 5 additions & 5 deletions docs/docs/reference/other-new-features/opaques.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: "Opaque Type Aliases"
Opaque types aliases provide type abstraction without any overhead. Example:

```scala
object Logarithms:
object MyMath:

opaque type Logarithm = Double

Expand All @@ -27,21 +27,21 @@ object Logarithms:
def + (y: Logarithm): Logarithm = Logarithm(math.exp(x) + math.exp(y))
def * (y: Logarithm): Logarithm = x + y

end Logarithms
end MyMath
```

This introduces `Logarithm` as a new abstract type, which is implemented as `Double`.
The fact that `Logarithm` is the same as `Double` is only known in the scope where
`Logarithm` is defined which in the above example corresponds to the object `Logarithms`.
`Logarithm` is defined which in the above example corresponds to the object `MyMath`.
Or in other words, within the scope it is treated as type alias, but this is opaque to the outside world
where in consequence `Logarithm` is seen as an abstract type and has nothing to do with `Double`.

The public API of `Logarithm` consists of the `apply` and `safe` methods defined in the companion object.
They convert from `Double`s to `Logarithm` values. Moreover, an operation `toDouble` that converts the other way, and operations `+` and `*` are defined as extension methods on `Logarithm` values.
The following operations would be valid because they use functionality implemented in the `Logarithms` object.
The following operations would be valid because they use functionality implemented in the `MyMath` object.

```scala
import Logarithms.Logarithm
import MyMath.Logarithm

val l = Logarithm(1.0)
val l2 = Logarithm(2.0)
Expand Down