DEV Community

Adam Cervenka
Adam Cervenka

Posted on

Scala Tip: appending a sequence optionally

If you want to add option's content to a sequence, you can easily do that using ++ or appendAll method.

Image 2021-07-29 at 15.18.26

This is possible because ++ expects collection and there is an implicit conversion from an option to collection in Scala.

But be careful with :+ because that expects just one element and no conversion will be applied and this happens:

Image 2021-07-29 at 15.21.13

Top comments (0)