Key Path Expressions as Functions and Identity Key Path

The SE-0249 Proposal gives the following code as an example, but it doesn't compile.

[1, nil, 3, nil, 5].compactMap(\.self)
// error: cannot convert value of type 'WritableKeyPath<Int, Int>' to expected argument type '(Int?) throws -> ElementOfResult?'

Is this a known bug?

1 Like

Yes, it's a known limitation, but I don't have a bug number offhand. I'll let others fill in the "why".

1 Like

SR-12897 is the bug you are looking for.

1 Like

Duplicate:

https://bugs.swift.org/browse/SR-12387

1 Like