https://kotlinlang.org logo
Title
x

xenoterracide

07/01/2019, 4:45 PM
var objectMapper: ObjectMapper = {
            val mapper = ObjectMapper()
            mapper.registerModules(JavaTimeModule(), KotlinModule())
            mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
            mapper
        }
is there a way to do an initialization block kind of like this? just
init
? is there something more per property?
m

Marc Knaup

07/01/2019, 4:46 PM
Just add
()
to the end to execute immediately.
p

Pere Casafont

07/01/2019, 4:47 PM
val objectMapper: ObjectMapper = ObjectMapper().apply {
  registerModules(JavaTimeModule(), KotlinModule())
  disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
  setSerializationInclusion(JsonInclude.Include.NON_NULL)
        }
😀 1
s

streetsofboston

07/01/2019, 4:47 PM
var objectMapper = ObjectMapper().apply {
            registerModules(JavaTimeModule(), KotlinModule())
            disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            setSerializationInclusion(JsonInclude.Include.NON_NULL)
        }
p

Pere Casafont

07/01/2019, 4:47 PM
@streetsofboston 🙈
m

Marc Knaup

07/01/2019, 4:47 PM
☝️ That would be more idiomatic Kotlin 🙂
Pere has
val
and Anton has omitted the redundant variable type. +1 for both, still a tie 😄
❤️ 2
p

Pere Casafont

07/01/2019, 4:49 PM
@xenoterracide also please prefer to use
val
everywhere you can and
var
only when you really need to be able to alter its value in the future. In your use case
var
smells a bit 😉
x

xenoterracide

07/01/2019, 4:51 PM
@Pere Casafont yeah I am, in this case this is going in a builder so the final thing can be immutable 😉
p

Pere Casafont

07/01/2019, 4:51 PM
awesome!
m

Marc Knaup

07/01/2019, 4:54 PM
The variable will be immutable, not the object it refers to 🙂
x

xenoterracide

07/01/2019, 4:59 PM
of course
still
anyways, thanks guys