Skip to content

[SR-8401] Generic class, type aliases and type inference at compile for a static function on a generic class #50926

@allenhumphreys

Description

@allenhumphreys
Previous ID SR-8401
Radar None
Original Reporter @allenhumphreys
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 4.1 Xcode 9.4.1

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

md5: cd7c569a6c30091ff8deac1ccd6486bd

duplicates:

Issue Description:

I'm not sure if this is a bug, but it kind of threw me for a loop so I thought I'd pose the question.

class CallWrapper<T, U: Decodable> {

    static func function() -> CallWrapper<Float, Float> {
        return CallWrapper() // Compiler Error: Cannot convert return expression of type 'SubCall<T>' to return type 'SubCall<Float>'
        // Fixit: Insert ' as! SubCall<Float>'
    }

    typealias TypeAlias<V, W: Decodable> = CallWrapper<V, W>

    static func function2() -> TypeAlias<Float, Float> {
        return TypeAlias()
    }
}

With function, I kind of expect type inference to pick up the return type of the function and use it for the initializer. But I understand kind of why it maybe shouldn't or can't because the type of the initializer in the static context is determined at the call site, i.e. CallWrapper<Int, Int>.function().

Things got strange for me, though, when I made a type alias, as in function2, where I was able to use type inference to only specify the complete type as the return of the function. My goal is convenience because in my case the types are pretty long and using the inference makes it easy to change the code.

I don't understand why type inference could work with the alias but not with the bare type, and was curious if one or the other is a bug.

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