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 Prag Prog topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Latest in PragProg

View all threads ❯