dhmitchell

dhmitchell

Kotlin Coroutine Confidence: not using kotlin style builder pattern (page 19ish)

To reinforce kotlin patterns, instead of

 ​val​ astronomyService = Retrofit.Builder()​   
      .client(createHttpClient().build())
   ​   .baseUrl(​"https://api.nasa.gov/planetary/"​)​   
      .addConverterFactory(MoshiConverterFactory.create())
   ​   .build().create<AstronomyService>()

why not the kotlin builder pattern?

 ​val​ astronomyService = Retrofit.Builder()​.apply {   
         client = createHttpClient().build()
   ​      baseUrl = ​"https://api.nasa.gov/planetary/"​)
         converterFactory += MoshiConverterFactory.create()
   ​   }
     .build().create<AstronomyService>()
// Or, if those don't have setters
 ​val​ astronomyService = Retrofit.Builder()​.apply {   
         client(createHttpClient().build())
   ​      baseUrl(​"https://api.nasa.gov/planetary/"​)​   
         addConverterFactory(MoshiConverterFactory.create())
   ​   }
     .build().create<AstronomyService>()

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

I like this question, because it’s exactly the kind of thing I thought about a lot when putting together the Retrofit examples for the book. I like to sweat the small stuff!

This is definitely one where you can adjust the code to suit whatever feels most natural and maintainable to you as you work through the examples. In the book, I chose to use the “old-fashioned” builder approach because it makes the code one line shorter, and because I think it looks slightly tidier without the extra brackets.

Where Kotlin’s apply() function really shines is with classes that don’t already come with fluent builder functions or optional constructor arguments. It makes it easy to call several setter functions in a row without repeating the receiver over and over again. But since Retrofit already provides chainable builder functions to solve the same problem, I think it’s idiomatic to use them, even in Kotlin.

Thanks for asking about it! I love how Kotlin’s scope functions and default constructor arguments give us so many options for replacing Java-style fluent builders.

Popular Pragmatic Bookshelf topics Top

kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New