Skip to content

[SR-514] Generic type cannot construct itself with a transformed type #43131

@swift-ci

Description

@swift-ci
Previous ID SR-514
Radar None
Original Reporter mschumacher (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 25c05faa51facf7575dd488e4683f95d

duplicates:

Issue Description:

Example:

struct Wrapper<T> {
    let value: T
    
    func map<R>(closure: T -> R) -> Wrapper<R> {
        return Wrapper(value: closure(value))
    }
}

This code produces the error:

Cannot invoke initializer for type 'Wrapper<T>' with an argument list of type '(value: R)'

Though it makes sense that this error occurs, since within Wrapper's implementation, T is bound to a concrete type, this shouldn't happen. Expected behavior is that Wrapper<T> can be constructed with any type.

It's maybe worth noting that there is a work-around:

protocol WrapperType {
    typealias T
    
    var value: T { get }
}

extension WrapperType {
    func map<R>(closure: Self.T -> R) -> Wrapper<R> {
        return Wrapper(value: closure(self.value))
    }
}

struct Wrapper<T>: WrapperType {
    let value: T
}

But it's verbose and unintuitive.

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