really simple question. Trying to create a java ob...
# getting-started
w
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
Possible, but you should probalby go for
Copy code
val jsonHeader = HttpHeaders().apply {
    accept = listOf(MediaType.APPLICATION_JSON)
}
โž• 1
๐Ÿ‘ 3
w
Nice. Thanks
That also cleans up some when blocks I had so thats a bonus.
f
real kotlin programmers just try let/accept/also/โ€ฆ until it works
๐Ÿ˜ 1
๐ŸงŒ 2
t
or until this works ๐Ÿ˜‰
๐Ÿ˜‚ 2
Especially the table in the end
t
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
I've mostly just been writing java like code. I guess.
t
@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
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
@Tobias Berger Oh you're right, thanks
w
I would like scala but not with the other people I work with.
โž• 1