Hello, how i can do something like that: ``` inlin...
# announcements
a
Hello, how i can do something like that:
Copy code
inline fun <reified T>  execute(): T {
   
    return doRequest()
}

private  inline fun <reified T> doRequest(): T
{
   //doSomething
}
Get the error "Public API inline function cannot access non public API" How to solve it?
d
A call to an inline function is replaced by it's body, so if someone calls
execute
that call is replaced by
doRequest
(since that's what
execute
does). For that to work
doRequest
must be accessible to whoever might call
execute
, which in this case is everyone. You can make
doRequest
internal
and annotate it with
@PublishedApi
- note however that you must then treat
doRequest
like a public method, you can't change it without breaking your API.
Alternatively you could not use
reified
for
doRequest
, but instead pass
T::class
from
execute
.
j
Sorry, this is probably a noob question but how does passing T::class help? I'm probably missing the point of having this inlined in the first place or something 😕
l
Use
@PublishedApi internal
instead of
private
.
d
Using
inline
is required here, since you are using
reified
. But if you just need the
KClass<T>
(or
Class<T>
, you can make
doRequest
not inline and take a
Class<T>
parameter instead.
Nvm
that doesn't help here.
sorry
a
Finnaly i have something like that and i wanna create public method for execute it.
Copy code
inline fun <reified T>  execute(): T {
   
    return doRequest()
}

private  inline fun <reified T> doRequest(): T
{
    fuelManager(token) //companion object for authorization
    val request = when(httpType)
    {
        <http://HttpType.POST|HttpType.POST> -> <http://Fuel.post|Fuel.post>(url, httpParamas).request
        HttpType.GET  -> Fuel.get(url,  httpParamas).request
    }

    val (response, error) =  request.timeoutRead(TIMEOUT).responseString().third

   return extractAuthResult(response, error, url)

}

private inline fun <reified T> extractResult(response: String?, error: FuelError?, url: String): T
{
    if(error != null || response.isNullOrEmpty() || response.contains("error") ){

        throw HTTPException("url=$url\r\nresponse = $response")
    }

    return try {  Gson().fromJson(response) }
    catch(e: JsonParseException)
    {
        throw HTTPException("Gson Error\r\nurl=$url\r\nreason = $e")
    }
}

private inline fun <reified T> Gson.fromJson(json: String) = this.fromJson<T>(json, object: TypeToken<T>() {}.type)
Any ideas?)
l
Yes @Alexjok, look at my message above
d
You are using a lot of inline there, this will blow up your code size. I highly recommend creating the
TypeToken
in the public facing function and then passing it to the others.
a
Okay, thanks all