https://kotlinlang.org logo
Title
a

Ayden

06/26/2018, 2:06 PM
Is there a way to write this line of code in a better way?
val httpLoggingInterceptor = HttpLoggingInterceptor().level = HttpLoggingInterceptor.Level.BODY
a

Andreas Sinz

06/26/2018, 2:07 PM
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
    level = HttpLoggingInterceptor.Level.BODY
}
a

Ayden

06/26/2018, 2:08 PM
The word of
apply
is telling to carry out another action after declare the variable?
a

Andreas Sinz

06/26/2018, 2:09 PM
yes, you are `apply`ing additional code to the instance
a

Ayden

06/26/2018, 2:10 PM
Thanks.
b

breandan

06/27/2018, 12:56 AM
You can also statically import
HttpLoggingInterceptor.Level.BODY
so it becomes
val httpLoggingInterceptor = HttpLoggingInterceptor().apply { level = BODY }
c

chi

06/27/2018, 6:55 PM
What's wrong with the way it is written in the question?
b

breandan

07/07/2018, 6:41 PM
Nothing. Just longer.