Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExpressibleByNilLiteral constraint to OptionalProtocol #805

Conversation

nkristek
Copy link
Contributor

@nkristek nkristek commented Oct 5, 2020

Hi, this is just a very small contribution regarding the OptionalProtocol and adds the ExpressibleByNilLiteral constraint to it. It is already implemented by Optional<Wrapped> and requires no further change. This makes it possible to initialise a value of type OptionalProtocol with nil instead of having to write .init(reconstructing: nil).

There also was a small typo in the documentation of the associatedType which I fixed as well.

Example

extension Property where Value: OptionalProtocol {
    func mapNil() -> Property<Value> {
        // now possible to write
        return Property(value: nil)
        // instead of
        return Property(value: .init(reconstructing: nil))
    }
}

Extending OptionalProtocol with the implementation of ExpressibleByNilLiteral is not possible due to a language constraint:

/// error: Extension of protocol 'OptionalProtocol' cannot have an inheritance clause
extension OptionalProtocol: ExpressibleByNilLiteral {
    
}

For now, as a workaround, one needs to append ExpressibleByNilLiteral to the generic constraint like so:

extension Property where Value: OptionalProtocol & ExpressibleByNilLiteral {
    func mapNil() -> Property<Value> {
        return Property(value: nil)
    }
}

Checklist

  • Updated CHANGELOG.md.

Niclas Kristek added 3 commits October 5, 2020 13:12
Optional already implements this and this makes it possible to just write nil instead of .init(reconstructing: nil)
@nkristek nkristek changed the title OptionalProtocol expressiblebynilliteral Add ExpressibleByNilLiteral constraint to OptionalProtocol Oct 6, 2020
@Marcocanc Marcocanc merged commit c9b8fa5 into ReactiveCocoa:master Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants