https://kotlinlang.org logo
#getting-started
Title
# getting-started
w

Wesley Acheson

09/03/2020, 11:54 AM
really simple question. Trying to create a java object that requires setters to be called. Is this the right pattern to use or not
Copy code
val jsonHeader = HttpHeaders().let {
        it.accept = listOf(MediaType.APPLICATION_JSON)
        it
    }
t

Tobias Berger

09/03/2020, 12:00 PM
Possible, but you should probalby go for
Copy code
val jsonHeader = HttpHeaders().apply {
    accept = listOf(MediaType.APPLICATION_JSON)
}
1
👍 3
w

Wesley Acheson

09/03/2020, 12:00 PM
Nice. Thanks
That also cleans up some when blocks I had so thats a bonus.
f

frogger

09/03/2020, 12:26 PM
real kotlin programmers just try let/accept/also/… until it works
😁 1
🧌 2
t

Tobias Berger

09/03/2020, 12:51 PM
or until this works 😉
😂 2
Especially the table in the end
t

Tobias Berger

09/03/2020, 12:53 PM
seriously though: let/also/apply/run can be really useful. Don't guess, learn what they do and use the one you think is most apropriate for your code
w

Wesley Acheson

09/03/2020, 12:54 PM
I've mostly just been writing java like code. I guess.
t

Tobias Berger

09/03/2020, 12:58 PM
@Cyril Truchi I actually prefer the official documentation: https://kotlinlang.org/docs/reference/scope-functions.html which got a lot better since that article was written
@Wesley Acheson I'm sure you'll get into the kotlin style. Trust me, you will not want to go back to java once you got used to writing kotlin - even if it's just about all the
?
w

Wesley Acheson

09/03/2020, 1:01 PM
No I don't want to go back. I've had groovy people claim that.
I've had scala people claim that. (and nearly believed them)
🙂 1
c

Cyril Truchi

09/03/2020, 1:02 PM
@Tobias Berger Oh you're right, thanks
w

Wesley Acheson

09/03/2020, 1:02 PM
I would like scala but not with the other people I work with.
1
2 Views