https://kotlinlang.org logo
u

ursus

03/13/2023, 1:42 PM
Copy code
class NboAdapter(
    val idAdapter: ColumnAdapter<NboId, String>
    ...
}

val nboAdapter = NboAdapter(
	idAdapter = IdColumnAdapter(::NboId), <--------------- do away with ::NboId
    ...
)

class IdColumnAdapter<T : Id>(val factory: (String) -> T) : ColumnAdapter<T, String> {
    override fun decode(databaseValue: String): T = factory(databaseValue)
    override fun encode(value: T): String = value.value
}

interface Id : Comparable<Id> {
    val value: String
    override fun compareTo(other: Id): Int = value.compareTo(other.value)
}

data class NboId(override val value: String) : Id
is there a way to infer the
::NboId
constructor? I'm trying maybe with reified but that would require
T(value)
constructor which is not possible in kotlin/java without reflection, right? however this does exist
Copy code
inline fun <reified T : Enum<T>> EnumColumnAdapter(): EnumColumnAdapter<T> {
  return EnumColumnAdapter(enumValues())
}
is that some sort of magic? I thought
values
was only definied on the concrete enum
a

asdf asdf

03/13/2023, 3:31 PM
It is somewhat magic, as it's implemented as a compiler intrinsic
(jvm implementation:)
u

ursus

03/13/2023, 3:33 PM
okay so I'm out of luck?
or is there a way to help the type inference, to do the ctor
also I'm wondering if I were to not do concrete
NboId
but turn it around to somethinglike
Id<Nbo>
, but not sure if that would help yet
well, I know it would, but it has its downsides, so still looking to make the first sample work would be best
a

asdf asdf

03/13/2023, 3:55 PM
I don't believe there is a way to infer a constructor of a reified type unfortunately without reflection
There is a pretty hacky trick like this that could be used, however, its not really recommended as it is unsafe and relies on proper importing
u

ursus

03/13/2023, 4:00 PM
that kinda melts my brain 😄
btw how about
value classes
, those can have only a single value right? maybe a ctor is accessible there generically?
a

asdf asdf

03/13/2023, 4:07 PM
Don’t think so either unfortunately, at once point was hoping there could be something like a
Valued<T>
interface that value classes would automatically implement that would allow for generic destructuring of them
u

ursus

03/13/2023, 4:07 PM
EXACTLY, same as the enum magic
Plan B is to run it throught json serializer, kotlinx has a support for value classes but yea thats not great
nevermind, thank you!
👍 1
3 Views