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

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

Closed
swift-ci opened this issue Jan 11, 2016 · 3 comments
Closed

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

swift-ci opened this issue Jan 11, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

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:

  • SR-1789 Swift can't infer types in generic methods that reuse part of the struct/class' parameters

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.

@belkadan
Copy link
Contributor

A simpler workaround: just specify <R> explicitly:

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

But I agree that the original code should work.

@Dante-Broggi
Copy link
Contributor

This still occurs in the Xcode 10 toolchain.

@belkadan
Copy link
Contributor

Forward-duping because there's more discussion and other incoming dups on SR-1789.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants