``` 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