Skip to content

Commit b746872

Browse files
authored
Add code tabs for _tour/extractor-objects (#2535)
1 parent 62a2a67 commit b746872

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

_tour/extractor-objects.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ redirect_from: "/tutorials/tour/extractor-objects.html"
1212

1313
An extractor object is an object with an `unapply` method. Whereas the `apply` method is like a constructor which takes arguments and creates an object, the `unapply` takes an object and tries to give back the arguments. This is most often used in pattern matching and partial functions.
1414

15+
{% tabs extractor-objects_definition class=tabs-scala-version %}
16+
17+
{% tab 'Scala 2' for=extractor-objects_definition %}
1518
```scala mdoc
1619
import scala.util.Random
1720

@@ -31,28 +34,68 @@ customer1ID match {
3134
case _ => println("Could not extract a CustomerID")
3235
}
3336
```
37+
{% endtab %}
38+
39+
{% tab 'Scala 3' for=extractor-objects_definition %}
40+
```scala
41+
import scala.util.Random
42+
43+
object CustomerID:
44+
45+
def apply(name: String) = s"$name--${Random.nextLong}"
46+
47+
def unapply(customerID: String): Option[String] =
48+
val stringArray: Array[String] = customerID.split("--")
49+
if stringArray.tail.nonEmpty then Some(stringArray.head) else None
50+
51+
val customer1ID = CustomerID("Sukyoung") // Sukyoung--23098234908
52+
customer1ID match
53+
case CustomerID(name) => println(name) // prints Sukyoung
54+
case _ => println("Could not extract a CustomerID")
55+
```
56+
{% endtab %}
57+
58+
{% endtabs %}
3459

3560
The `apply` method creates a `CustomerID` string from a `name`. The `unapply` does the inverse to get the `name` back. When we call `CustomerID("Sukyoung")`, this is shorthand syntax for calling `CustomerID.apply("Sukyoung")`. When we call `case CustomerID(name) => println(name)`, we're calling the unapply method with `CustomerID.unapply(customer1ID)`.
3661

3762
Since a value definition can use a pattern to introduce a new variable, an extractor can be used to initialize the variable, where the unapply method supplies the value.
3863

64+
{% tabs extractor-objects_use-case-1 %}
65+
66+
{% tab 'Scala 2 and 3' for=extractor-objects_use-case-1 %}
3967
```scala mdoc
4068
val customer2ID = CustomerID("Nico")
4169
val CustomerID(name) = customer2ID
4270
println(name) // prints Nico
4371
```
72+
{% endtab %}
73+
74+
{% endtabs %}
4475

4576
This is equivalent to `val name = CustomerID.unapply(customer2ID).get`.
4677

78+
{% tabs extractor-objects_use-case-2 %}
79+
80+
{% tab 'Scala 2 and 3' for=extractor-objects_use-case-2 %}
4781
```scala mdoc
4882
val CustomerID(name2) = "--asdfasdfasdf"
4983
```
84+
{% endtab %}
85+
86+
{% endtabs %}
5087

5188
If there is no match, a `scala.MatchError` is thrown:
5289

53-
```scala
90+
{% tabs extractor-objects_use-case-3 %}
91+
92+
{% tab 'Scala 2 and 3' for=extractor-objects_use-case-3 %}
93+
```scala mdoc:crash
5494
val CustomerID(name3) = "-asdfasdfasdf"
5595
```
96+
{% endtab %}
97+
98+
{% endtabs %}
5699

57100
The return type of an `unapply` should be chosen as follows:
58101

0 commit comments

Comments
 (0)