Skip to content

[SR-6554] Type inference error when applying optional function to optional value #49104

@swift-ci

Description

@swift-ci
Previous ID SR-6554
Radar None
Original Reporter trimm (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 4.0, Swift 4.0.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.0Regression
Assignee None
Priority Medium

md5: d5270fb56c4a13ba96b13a49017fdb53

Issue Description:

public extension Optional {
    public func apply<Result>(_ transform: ((Wrapped) -> Result)?) -> Result? {
        return self.flatMap { value in
            transform.map { $0(value) }
        }
    }

    public func apply<Value, Result>(_ value: Value?) -> Result?
        where Wrapped == (Value) -> Result
    {
        return value.apply(self)
    }
}

Possibly related to bug: https://bugs.swift.org/browse/SR-6509

This works fine under Swift3-3.2.

Actual results:

'apply' produces 'Result?', not the expected contextual result type 'Result?'

Expected results:

The code compiles, compiler infers the type of result for type of Wrapped

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfregressionswift 4.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions