jw
01/07/2019, 7:33 PM: Enum<T>
which (combined with inline
) lets me do T -> String
and String -> T
conversion. It would be nice to achieve similar T -> V
and V -> T
conversions with an inline type (where V
is the property type) in a generic way. Right now I'm using a factory where you have to pass ::Foo
and Foo::property
as arguments which leaves a lot to be desired.karelpeeters
01/07/2019, 7:52 PMjw
01/07/2019, 7:52 PMkarelpeeters
01/07/2019, 7:53 PMjw
01/07/2019, 7:54 PMkarelpeeters
01/07/2019, 7:59 PMkarelpeeters
01/07/2019, 7:59 PMjw
01/07/2019, 8:00 PMorangy
Inline<T>
and require users to implement the interface of they want automatic handling. The problem with this is that backing val will have to have same name.karelpeeters
01/07/2019, 8:06 PMjw
01/07/2019, 8:07 PMorangy
orangy
orangy
karelpeeters
01/07/2019, 9:27 PMorangy
orangy
orangy
karelpeeters
01/07/2019, 9:30 PMorangy
raulraja
01/07/2019, 9:35 PMraulraja
01/07/2019, 9:35 PM/** new type **/
inline class Id(val value: String)
/** proof there is an isomorphism between [A] and [B] **/
interface Iso<A, B> {
fun from(a: A): B
fun to(b: B): A
}
/** proof [Id] <-> [String] **/
extension class IdStringIso : Iso<String, Id> {
override fun from(a: String): Id = Id(a)
override fun to(b: Id): String = b.value
}
/** For All [A] for which there is an isomorphic [B] provide a conversion function **/
fun <A, B> A.convert(with iso: Iso<A, B>): B =
to<B>()
/** This compiles because finds proof in `IdStringIso` **/
val id : Id = "000".convert()
raulraja
01/07/2019, 9:36 PMTo<A, B>
more constrained interfaceraulraja
01/07/2019, 9:37 PMraulraja
01/07/2019, 9:38 PMcomponent1
override that let's you destructure the conversion for all `A`s such as that:
val (value) = id
raulraja
01/07/2019, 9:40 PMval (id) = value
jw
01/07/2019, 9:40 PMIdStringIso
at all is a loss for me. I already have an interface representing a converter and I'm trying to create a single implementation which can be used for any inline class (like I can do for enums)