Skip to content

[SR-11472] Generic parameter inference problem #53872

@jeremyabannister

Description

@jeremyabannister
Previous ID SR-11472
Radar None
Original Reporter @jeremyabannister
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 3094e6c47b27e8d301bf4f2a2ef9c7e9

duplicates:

Issue Description:

This code compiles with Swift 5:

struct A <X> {
    
    let dict: [Int: X]
    
    init (dict: [Int: X]) {
        self.dict = dict
    }
    
    func map <Y> (_ transform: (X)->Y) -> A<Y> {
        return A<Y>(dict: dict.mapValues { transform($0) })
    }
}

but this code does not:

struct A <X> {
    
    let dict: [Int: X]
    
    init (dict: [Int: X]) {
        self.dict = dict
    }
    
    func map <Y> (_ transform: (X)->Y) -> A<Y> {
        return A(dict: dict.mapValues { transform($0) })
    }
}

The only difference is that I have removed the explicit type from the construction of A in the map method. Of course the generic parameter is already specified in two ways, one is the return type and the other is the fact that the transform spits out values of type Y. What's going on here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeResolverbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfexpressionsFeature: expressionsgenericsFeature: generic declarations and typesswift 6.0swift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposaltype checkerArea → compiler: Semantic analysistype inferenceFeature: type inferenceunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions