Skip to content

decode(_:from:) not working when using Int #81

@vdkdamian

Description

@vdkdamian

Issue

I have a function with the following Swift code:

let urlRequest = try self.urlRequest(.get, request: .products, .count, for: .core)
let (data, urlResponse) = try await session.data(for: urlRequest)
        
return try APIManagerDecoder.decode(Int.self, from: data)

This gets transpiled to the following Kotlin code:

val urlRequest = this.urlRequest(ProjectAPI.Method.get, ProjectAPI.Endpoint.products, ProjectAPI.Endpoint.count, for_ = APIManager.TokenOwner.core)
val (data, urlResponse) = session.data(for_ = urlRequest)

return@l APIManagerDecoder.decode(Int::class, from = data)

I'm getting an error from the compiler that refers to the following line:
return@l APIManagerDecoder.decode(Int::class, from = data)

Argument type mismatch: actual type is 'KClass', but 'KClass<uninferred T (of fun decode)>' was expected.

Valid workaround:

I was able to find a workaround as follows:

let urlRequest = try self.urlRequest(.get, request: .products, .count, for: .core)
let (data, urlResponse) = try await session.data(for: urlRequest)
        
return try APIManagerDecoder.decode(IntWrapper.self, from: data).value

Where this is the 'IntWrapper' struct:

struct IntWrapper: Decodable {
    
    var value: Int
    
    init(_ value: Int) {
        self.value = value
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        self.value = try container.decode(Int.self)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions