It's hard for me to understand the pushback to this. A filter
method is already available on many types in the stdlib. Those types also have map
/flatMap
as Optional
does. To me this looks like a obvious operation that Optional
can and should support without confusion or undue bloat.
I think most people here agree and are just looking for better motivation and real world use-cases to justify this addition.
Pushbacks don't invalidate a pitch. But shouldn't this thread be moved to the Evolution/Pitches category, according to the Swift Evolution Process?
Speaking on behalf of my team, I use Optional.flatMap pretty consistently, and just about every junior dev that comes in has a real hard time understanding what flatMap has to do with the outcome. I think filter is closer to the intent of "if this optional isn't nil, then do this."
am i the only one here who sees Array.map
, Array.flatMap
, and Array.filter
as distinct from Optional.map
and Optional.flatMap
?
I use array map when I want to apply a function to everything in the array. I use array filter to conditionally get rid of stuff in an array.
I use optional map because ?.
doesn’t work inside parentheses. If optional filter existed i would use it to get rid of stuff in an optional. but an optional only ever has at most one thing in it, which I can already get rid of with flatMap. So there’s no benefit to the vectorized variant.
Is optional reduce next? what does optional reduce even mean?
This is a fair point. Really I think it'd be very clear if we were able to do something like:
let a: String? = nil
a?.do { print($0) }
let a:String? = nil
a.map{ print($0) }
?
It certainly works, though map and flatMap are transformations, which I think is why it's confusing to newer devs. I'd imagine they get a rough comment in their PR if they used Array.map to print all elements in the array instead of Array.forEach.
Reading above, I'm now realizing I misread what Alexander pitched in the first place, and am talking about a different problem, which is needing a better shorthand for the opposite of ?? (i.e. "if item is not nil, then do this) than .flatMap or .map on optional.
The argument that Optional.flatMap
invalidates the need for Optional.filter
is not compelling; by that logic, Sequence.filter
is unnecessary by Sequence.compactMap
or even Sequence.flatMap
.
// The following are functionally equivalent:
sequence.filter(shouldKeep)
sequence.compactMap { shouldKeep($0) ? $0 : nil }
sequence.flatMap { shouldKeep($0) ? [$0] : [] }
filter
holds notable advantages over performing the same operation using flatMap
:
- Because
filter
does not perform a transformation on the wrapped value, call site clarity improves. Upon readingfilter
, the intent of the operation is clear: the wrapped value is either kept or discarded; it is not transformed. - Using
flatMap
asfilter
typically involves the use of the ternary operator, which increases cognitive load.
For example, which of the following is easier to read at a glance?
let effectiveText = textField.text.flatMap { !$0.isEmpty ? $0 : nil }
let effectiveText = textField.text.filter { !$0.isEmpty }
The gain of Optional.filter
exists principally on the grounds of readability, not on difficulty of implementation, and its inclusion in the Standard Library should be judged as such.
Is optional reduce next? what does optional reduce even mean?
This topic is only tangentially related, so I won't go into much detail here. In short, reduce
does exist on Optional
. It looks like this:
func reduce<Output>(
_ initialValue: Output,
_ combine: (Output, Wrapped) throws -> Output
) rethrows -> Output {
switch self {
case .some(let value):
return combine(initialValue, value)
case .none:
return initialValue
}
}
For instance, if you have an Int
and an Optional<Int>
and you'd like to take the minimum of the two (or just use the first one if the second is nil
), you might write
let minimum = optionalInt.reduce(nonOptionalInt, min)
I think of them as the same. A map
turns an array of Foo
into an array of Bar
. Or an optional Foo
into an optional Bar
.
flatMap
does the same, except it also removes a layer through the transform. Same for Promise
s, Result
, etc. I see no difference.
In fact, I hope we some day can just declare them all to be conforming to some common protocol, wether we call that protocol Monad
or Mappable
or whatever, I don’t care too much about.
In fact, we could then implement filter
on a default protocol extension through the confirming type’s flatMap
implementation, as long as we also had a static empty
element defined.
Array’s filter could easily be implemented as
func filter(_ isIncluded: (Element) -> Bool) -> [Element] {
return flatMap { isIncluded($0) ? [$0] : [] }
}
i was about to fight but actually that sounds like a pretty useful operation. if filter
does end up getting added to optional we should add reduce
too
I feel like the original pitch was much more to the point. I don’t think it’s necessary to muddy the waters with excessive motivation discussion and alternative considered sections that only invite confusion. This pitch isn’t about inventing a novel solution to Swift specific problem. This is about including the batteries.
@AlexanderM I advise you significantly down pair the proposal: Short description and reference to the plethora of prior art is all that’s needed here.
I like to think of Optional as a collection of zero or one elements, so filter fits perfectly in my mental model. +1
Well, I guess I'm suggesting it doesn't need extensive effort to justify, if we agree that this is a simple and obvious addition of "batteries" (as Pavol aptly put it).
okay this is not exactly related to the proposal which i am /slightly/ in favor,, but can i just say the whole batteries thing has been taken way out of context. Optional.filter
is not a battery, it’s just sugar (which I think could still stand on its own). Batteries are common things that are hard to do (correctly) yourself and would benefit from a standard implementation. Examples of missing batteries in Swift include
- Printing a floating point number to however many decimal places
- Generating a random vector
- Trimming whitespace from a string
- Splitting a string into lines
Optional.filter
is not a battery.
am i the only one here who sees
Array.map
,Array.flatMap
, andArray.filter
as distinct fromOptional.map
andOptional.flatMap
?
I use optional map because
?.
doesn’t work inside parentheses.
This is close to what I was thinking.
I consider Optional.map
to be a hacky mechanism for using functions after an instance, in a way that isn't possible for non-optional types, which require the function before an instance.
var instance: Class?
instance = instance.map {
$0.property1 = "😺"
$0.property2 = "🐈"
return $0
}
instance = {
$0.property1 = "😺"
$0.property2 = "🐈"
return $0
} ( Class() )
Additionally, Optional
can be used as a hack around not being able to extend Any
. That's what I see from this proposal: that all of us who would like this, actually want filter
for all types, not just optionals (though the name would probably have to be altered, to avoid ambiguity for multidimensional sequences). Unfortunately, you can't do that now without extending all the types you're going to use, to adopt a protocol, like this:
protocol Filterable { }
extension Filterable {
func filter(_ shouldKeep: (Self) throws -> Bool) rethrows -> Self? {
return try shouldKeep(self) ? self : nil
}
}
extension String: Filterable {}
"🌫".filter { $0.isOnlyWhiteSpace } ?? ""
let string: String? = nil
string?.filter { $0.isOnlyWhiteSpace } ?? ""
I think it would be a much better idea to shelve this proposal, work on making it possible to extend Wrapped
(i.e. Any
), and then add more functions to Any
directly. Otherwise, as Optional
grows more powerful, people are eventually just going to stop using non-optionals entirely.
Wat?
This is the consequence of years of exposition to fluent interfaces, pipes, functional idioms... and the fact that if
is a statement. People like linear imperative code:
let result = start.one.two().three { ... }.four { ... }
vs.
let result = Four(Two(start.one).three)
I cannot follow your reasoning. When every other language with Optional includes this method and Swift doesn’t, this is about !