``` var objectMapper: ObjectMapper = { ...
# announcements
x
Copy code
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
Just add
()
to the end to execute immediately.
p
Copy code
val objectMapper: ObjectMapper = ObjectMapper().apply {
  registerModules(JavaTimeModule(), KotlinModule())
  disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
  setSerializationInclusion(JsonInclude.Include.NON_NULL)
        }
😀 1
s
Copy code
var objectMapper = ObjectMapper().apply {
            registerModules(JavaTimeModule(), KotlinModule())
            disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            setSerializationInclusion(JsonInclude.Include.NON_NULL)
        }
p
@streetsofboston 🙈
m
☝️ 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
@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
@Pere Casafont yeah I am, in this case this is going in a builder so the final thing can be immutable 😉
p
awesome!
m
The variable will be immutable, not the object it refers to 🙂
x
of course
still
anyways, thanks guys