Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit a975b20

Browse files
authored
Add exercise opaque type aliases (#121)
* Add slide for extra exercise on opaque type aliases * Rename exercise on opaque type aliases - Make it apparent in the exercise name that this is an optional exercise * Checkpoint result of running 'ctma renumber-exercises -f 9 -t 10 -s 1' * Checkpoint result of running 'ctma duplicate-insert-before -n 9' * Add exercise and instructions - Extra exercise on opaque type aliases and comparing them to other alternatives such as value classes * Checkpoint result of running 'ctma renumber-exercises -f 13 -t 20 -s 1'
1 parent 258dc3b commit a975b20

File tree

85 files changed

+1350
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1350
-2
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Exploring Opaque type aliases
2+
3+
## Background
4+
5+
In the chapter on Opaque type aliases, we did a quick review of the various features in
6+
Scala that allow us to abstract over certain types without incurring the overhead of
7+
boxing/unboxing. We saw that some of these features (Value classes for example) fail
8+
to achieve this goal under certain circumstances.
9+
10+
In this exercise, you will look at the aforementioned features and have a look at the
11+
generated byte code to see whether boxing occurs or not.
12+
13+
## Steps
14+
15+
- Clone this repo: [Moving from Scala 2 to Scala 3](https://github.com/lunatech-labs/lunatech-scala2-to-scala3-course)
16+
17+
- In the cloned repository, `cd` into the `code-snippets` folder
18+
- This folder holds a multi-project `sbt` build. Load this project into your IDE (IntelliJ
19+
or VSCode with Metals)
20+
- We will focus on the `opaque-type-aliases` `sbt` project for this exercise
21+
22+
- Start an `sbt` session in the `code-snippets` folder and compile the project
23+
24+
We will now walk through the different features
25+
26+
### Case class wrappers
27+
28+
- Have a look at the source code in the `CaseClasses.scala` file in the `opaquetypelaliases.caseclasses` package
29+
- The Scala compiler has generated class files for this under the `target/scala-3.3.0/classes/opaquetypealiases/caseclasses` folder
30+
- Decompile the different class files using:
31+
- VSCode with Metals: the `cfr` Java decompiler: right-click on a class file. Select `Metals Analyse Source`/`Show decompiled with CFR`
32+
- IntelliJ: right-click on a class file. Select `Show Decompiled Class As Java`
33+
- Look at the decompiled code and figure out whether boxing occurs or not.
34+
35+
### Value-class wrappers
36+
37+
- Repeat the same steps for file `ValueClasses.scala` in the `opaquetypelaliases.valueclasses` package
38+
39+
- Repeat for `ParametricPolymorphism.scala` in the `opaquetypelaliases.parametricpolymorphism` package
40+
41+
- Repeat for `Subtyping.scala` in the `opaquetypelaliases.subtyping` package
42+
43+
### Opaque type aliases
44+
45+
- Repeat the same steps for file `UsingTheAliases.scala` in the `opaquetypelaliases.opaquetypealias`
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)