Is there a workaround for this issue? <https://you...
# announcements
n
Is there a workaround for this issue? https://youtrack.jetbrains.com/issue/KT-34051
y
What's the code that's causing this error? Because I fiddled around with this a bit and I think I could possibly find a workaround depending on your specific case.
n
this is my sequence of calls…
Copy code
class CitySfRepository : BaseSfRepository() {
    override suspend fun cityById(id: Int): City? =
            getObject( "SELECT Id, ID__c, Name FROM $objectType WHERE ID__c = $id")
}
This
getObject
function is defined in the parent class
BaseSfRepository
Copy code
protected suspend inline fun <reified T> getObject(q: String): T? =
        suspendCoroutine { continuation ->
            sendAsync<List<T>?>(
                    RestRequest.getRequestForQuery(
                            ApiVersionStrings.VERSION_NUMBER,
                            q
                    ),
                    { list ->
                        continuation.resume(list?.first())
                    },
                    { exception ->
                        continuation.resumeWithException(exception)
                    }
            )
        }
In this
sendAsync
function I’m getting the response and parsing using Gson library
Copy code
protected inline fun <reified T> parseResponse(
            restResponse: RestResponse
    ): T? {
        val x = gson.fromJson<T?>(
                restResponse.asJSONObject().getJSONArray("records").toString(),
                object : TypeToken<T?>() {}.type
        )
        return x
    }
I’m getting this error:
Copy code
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.test.mycompany.City
y
This looks like an entirely different error I think. What line are you getting the cast exception on?
n
Looks like that the compiler misses the type T when I call
sendAsync<List<T>?>
. Because the Gson is returning a list of
List<LinkedTreeMap>
when it parses the JSON
The interesting part is: this function works… 🤷‍♂️
Copy code
// From CitySfRepository
    override suspend fun allCities(): List<City> =
            getObjectList("SELECT Id, ID__c, Name FROM $objectType ORDER BY Name") ?: emptyList()
And
getObjectList
passes the
T
directly…
Copy code
protected suspend inline fun <reified T> getObjectList(q: String): T? =
            suspendCoroutine { continuation ->
                sendAsync<T?>(
                        RestRequest.getRequestForQuery(
                                ApiVersionStrings.VERSION_NUMBER,
                                q
                        ),
                        { list ->
                            continuation.resume(list)
                        },
                        { exception ->
                            continuation.resumeWithException(exception)
                        }
                )
            }
[FIXED] I was answering my own question 🤦‍♂️ the list type is erased, so I
reified
it 😉
Copy code
protected suspend inline fun <reified T, reified J: List<T>> getObject(q: String): T? =
            suspendCoroutine { continuation ->
                sendAsync<J?>(
                        RestRequest.getRequestForQuery(
                                ApiVersionStrings.VERSION_NUMBER,
                                q
                        ),
                        { list ->
                            continuation.resume(list?.first())
                        },
                        { exception ->
                            continuation.resumeWithException(exception)
                        }
                )
            }
y
lol happens to the best of us. Nice job!
🎉 1