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.

Where Next?

Popular Pragmatic Bookshelf topics Top

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
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
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1030 17300 381
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
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
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

Sub Categories: