# Add \`filter\` and \`ifSome(then:)\` to Optional

**URL:** <https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143>\
**Category:** Pitches\
**Created:** [January 21, 2018, 8:01am UTC](https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143 "2018-01-21T08:01:59Z")\
**Posts on this page:** 1\
**Showing post:** 44

<div class="post-metadata">

**Author:** ![palimondo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/palimondo/32/1144_2.png) [@palimondo](https://forums.swift.org/u/palimondo)\
**Post date:** [August 1, 2018, 4:53pm UTC](https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143/44 "2018-08-01T16:53:43Z")

</div>

Let’s see if this has any legs then…

> <https://github.com/apple/swift/issues/50960>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-8437 |
> |Radar | None |
> |Original Reporter | @palimondo |
> |Type | New Feature |
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 0 |
> |Component/s | Standard Library |
> |Labels | New Feature, LanguageFeatureRequest |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: 3852b2408d69c13546d326891036b911
> 
> \</details\>
> 
> 
> 
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> There is a \[strong precedent from other programming languages\](https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143) that have optional types to include method named \`filter\` that conditionally maps the optional to \`nil\`. The implementation is trivial:
> 
> \`\`\` java
> extension Optional {
> func filter(\_ predicate: (Wrapped) throws -\> Bool) rethrows -\> Optional {
> return try flatMap { try predicate($0) ? self : nil }
> }
> }
> \`\`\`
> 
> Swift is currently missing this method. When pitched in Swift Evolution Forums, this method was met with no opposition. This looks like a clear omission without any downsides, that should be easy to add into Swift standard library without the need to go through lengthy Swift Evolution process.

Nope. @jrose said:

> API additions _always_ go through the Swift Evolution process.

@AlexanderM would you be fine with splitting out just the `filter` into its own proposal? Do you have time to do that?

BTW, this optional filter pattern is all over the [SE-0222](https://github.com/apple/swift-evolution/blob/master/proposals/0222-lazy-compactmap-sequence.md) that’s currently in review.

---

_[View the full topic](https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143)._
