Skip to content

Commit 4765032

Browse files
authored
Merge pull request #1983 from mariogalic/main
Conversion Between Option and Collection
2 parents e36fb38 + 8bd1405 commit 4765032

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
layout: multipage-overview
3+
title: Conversion Between Option and the Collections
4+
partof: collections-213
5+
overview-name: Collections
6+
7+
num: 18
8+
previous-page: conversions-between-java-and-scala-collections
9+
10+
permalink: /overviews/collections-2.13/:title.html
11+
---
12+
`Option` can be seen as a collection that has zero or exactly one element, and it provides a degree of interoperability with the collection types found in the package `scala.collection`. In particular, it implements the interface `IterableOnce`, which models the simplest form of collections: something that can be iterated over, at least once. However, `Option` does not implement the more comprehensive interface of `Iterable`. Indeed, we cannot provide a sensible implementation for the operation [`fromSpecific`](https://github.com/scala/scala/blob/6c68c2825e893bb71d6dc78465ac8c6f415cbd93/src/library/scala/collection/Iterable.scala#L173), which is supposed to create an `Option` from a collection of possibly more than one element. Starting from [Scala 2.13](https://github.com/scala/scala/pull/8038), `Option` was made an `IterableOnce` but not an `Iterable`.
13+
14+
Hence `Option` can be used everywhere an `IterableOnce` is expected, for example, when calling `flatMap` on a collection (or inside a for-comprehension)
15+
16+
```scala mdoc
17+
for {
18+
a <- Set(1)
19+
b <- Option(41)
20+
} yield (a + b)
21+
// : Set[Int] = Set(42)
22+
```
23+
24+
since the operation `flatMap` on the type `Set[Int]` takes a function returning an `IterableOnce`:
25+
26+
```
27+
def flatMap[B](f: Int => IterableOnce[B]): Set[B]
28+
```
29+
30+
Although `Option` does not extend `Iterable`, there exists an [implicit conversion](https://github.com/scala/scala/blob/6c68c2825e893bb71d6dc78465ac8c6f415cbd93/src/library/scala/Option.scala#L19) between `Option` and `Iterable`
31+
32+
33+
```
34+
implicit def option2Iterable[A](xo: Option[A]): Iterable[A]
35+
```
36+
37+
so although `Option[A]` is not a full collection it can be _viewed_ as one. For example,
38+
39+
```scala mdoc
40+
Some(42).drop(1)
41+
// : Iterable[Int] = List()
42+
```
43+
44+
expands to
45+
46+
```scala mdoc
47+
Option.option2Iterable(Some(42)).drop(1)
48+
// : Iterable[Int] = List()
49+
```
50+
51+
because `drop` is not defined on `Option`. A downside of the above implicit conversion is that instead of getting back an `Option[A]` we are left with an `Iterable[A]`. For this reason, `Option`’s documentation carries the following note:
52+
53+
> Many of the methods in `Option` are duplicative with those in the `Iterable` hierarchy, but they are duplicated for a reason: the implicit conversion tends to leave one with an `Iterable` in situations where one could have retained an `Option`.

_overviews/collections-2.13/conversions-between-java-and-scala-collections.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ overview-name: Collections
66

77
num: 17
88
previous-page: creating-collections-from-scratch
9+
next-page: conversion-between-option-and-the-collections
910

1011
languages: [ru]
1112
permalink: /overviews/collections-2.13/:title.html

0 commit comments

Comments
 (0)