edrd
08/20/2020, 3:20 PMpackage com.fasterxml.jackson.module.kotlin
import com.fasterxml.jackson.databind.ObjectMapper
companion object ObjectMapper.Kotlin {
fun forKotlin(): ObjectMapper = ...
}
// Would then also work
fun ObjectMapper.Kotlin.withAlphabeticallySortedProperties(): ObjectMapper = ...
// Usage from another file
import com.fasterxml.jackson.module.kotlin.ObjectMapper.Kotlin
fun main() = ObjectMapper.forKotlin()
This eliminates the need to add empty companion object
declarations in classes solely for the purpose of allowing extension functions and allows defining extensions for Java types.
The difference between internally and externally defined companion objects would be the ability to access private and protected members.
For more information on use cases and problems of the current alternatives, see my previous proposal (linked above).gildor
08/20/2020, 3:55 PMSame package of original ObjectMapperI don’t think it will work with Java 9+, where you cannot have the same package in different modules, so it limited to a single module it would be almost useless
gildor
08/20/2020, 3:56 PMgildor
08/20/2020, 3:58 PMedrd
08/20/2020, 4:02 PMedrd
08/20/2020, 4:09 PM// data/User.kt
package example.data
data class User(val name: String)
// serialization/User.kt
package example.serialization
import example.data.User
companion object User {
fun fromJson(content: String) = ...
}
// database/User.kt
package example.database
import example.data.User
companion object User {
fun fromResultRow(resultRow: ResultRow) = ...
}
// main.kt
package example
import example.data.User
import example.serialization.User.fromJson
import example.database.User.fromResultRow
fun main() = ...
edrd
08/20/2020, 4:10 PMedrd
08/20/2020, 4:12 PMedrd
08/20/2020, 4:15 PMgildor
08/20/2020, 4:18 PMbut I’m not sure about the impact of allowing multiple external companion object declarationsIt may work, same way as multiple extension functions imported for the same class, but if different companions have conflicting declarations, it make sense to allow rename companion, same as for extension function, so it will change syntax frrom ObjectMapper.forKotlin() to ObjectMapper.CustomName.forKotlin(), same as it works now with named companion object
gildor
08/20/2020, 4:19 PMgildor
08/20/2020, 4:20 PMgildor
08/20/2020, 4:20 PMfun ObjectMapper.Companion.withAlphabeticallySortedProperties
just works for objects even without companion object? it wouldn’t be possible to define real object, but at least it would allow to define extensionsedrd
08/20/2020, 4:20 PMcompanion object ObjectMapper
and then return ObjectMapper
in forKotlin
, but these are actually different types.edrd
08/20/2020, 4:22 PMedrd
08/20/2020, 4:23 PMedrd
08/20/2020, 4:25 PMCompanion
explicitly:
companion object ObjectMapper.Companion {
fun forKotlin(): ObjectMapper = ...
}
edrd
08/20/2020, 4:25 PMCompanion
edrd
08/20/2020, 4:27 PMcom.fasterxml.jackson.module.kotlin.ObjectMapper.Companion
edrd
08/20/2020, 4:28 PMgildor
08/20/2020, 4:30 PMObjectMapper.Companion.forKotlin(): ObjectMapper
edrd
08/20/2020, 4:31 PM.Companion
extension functions even without a companion object declaration?gildor
08/20/2020, 4:31 PMgildor
08/20/2020, 4:31 PMedrd
08/20/2020, 4:32 PMedrd
08/20/2020, 4:35 PMfun ObjectMapper::forKotlin
syntax and automatically creating companion objects, but I think allowing to define external companion objects would be more intuitivegildor
08/20/2020, 4:38 PMgildor
08/20/2020, 4:38 PMgildor
08/20/2020, 4:40 PMedrd
08/20/2020, 4:41 PM.Companion
could be an issueedrd
08/20/2020, 4:43 PMedrd
08/20/2020, 4:44 PMfun ObjectMapper.Companion.forKotlin
but would not be able to do println(ObjectMapper.Companion)
gildor
08/20/2020, 4:44 PMedrd
08/20/2020, 4:46 PMedrd
08/20/2020, 4:50 PMcompanion fun Foo.bar()
and companion val Foo.baz
but it would be confusing for Java types since there's no actual companion object involvedgildor
08/20/2020, 4:53 PMcould create companion objects only when there are extension functions defined for the given classIt will not work for libraries, also it cannot work for already created java classes
gildor
08/20/2020, 4:54 PMgildor
08/20/2020, 4:54 PMedrd
08/20/2020, 4:54 PMedrd
08/20/2020, 4:55 PMedrd
08/20/2020, 5:07 PMimport com.fasterxml.jackson.module.kotlin.ObjectMapper.Kotlin.forKotlin as forKotlinFoo
But I don't think factory methods, which are the main use case for this, are so common that conflicts would happen oftengildor
08/20/2020, 5:28 PMedrd
09/01/2020, 7:04 PMelizarov
09/02/2020, 9:24 AMedrd
09/02/2020, 12:13 PM