Skip to content

Commit fb1f19a

Browse files
committed
Specialised Option.tapEach to preserve the type
1 parent 78d6be6 commit fb1f19a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/main/scala/scala/Option.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ sealed abstract class Option[+A] extends IterableOnce[A] with Product with Seria
333333
@inline final def filterNot(p: A => Boolean): Option[A] =
334334
if (isEmpty || !p(this.get)) this else None
335335

336+
/** Apply the side-effecting function $f to the option's value,
337+
* if it is nonempty. Otherwise, do nothing.
338+
*
339+
* @param f a function to apply to the option's value
340+
* @tparam U the return type of f
341+
* @return this Option
342+
*/
343+
@inline final def tapEach[U](f: A => U): Option[A] = {
344+
foreach(f); this
345+
}
346+
336347
/** Returns false if the option is $none, true otherwise.
337348
*
338349
* This is equivalent to:

0 commit comments

Comments
 (0)