reactormonk
09/02/2022, 1:22 PMkotlin
@JsonClass(generateAdapter = true)
data class Gallery<T>(val galleryItems: List<GalleryItem<T>>)
@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed interface GalleryItem<T> {
@TypeLabel("photo")
@JsonClass(generateAdapter = true)
data class Photo<T>(val url: T): GalleryItem<T>
@TypeLabel("video")
@JsonClass(generateAdapter = true)
data class Video<T>(val url: T): GalleryItem<T>
}
And
val x: Gallery<String>
as well as
fun parse(x: String): HttpUrl
and I would like a Gallery<HttpUrl>
in the end. In Haskell, I'd add a deriving Functor
to the type declaration and then I could use all the fun combinatorics functions. How would I do that in Kotlin?simon.vergauwen
09/02/2022, 1:25 PMderiving Functor
๐
There is nothing like that sadly. Your best is to write a bit of boilerplate IMO.
fun <A, B> Gallery<A>.map(transform: (A) -> B)" Gallery<B> = map {
when(this) {
is Photo -> Photo(transform(url))
is Video -> Video(transform(url))
}
}
That of course doesn't give you all the other goodies from Functor
& coreactormonk
09/02/2022, 1:26 PMcopy
either ๐simon.vergauwen
09/02/2022, 1:27 PMsimon.vergauwen
09/02/2022, 1:27 PMreactormonk
09/02/2022, 1:27 PMsimon.vergauwen
09/02/2022, 1:27 PMsimon.vergauwen
09/02/2022, 1:27 PMreactormonk
09/02/2022, 1:28 PMsimon.vergauwen
09/02/2022, 1:29 PMsimon.vergauwen
09/02/2022, 1:30 PMreactormonk
09/02/2022, 1:30 PMreactormonk
09/02/2022, 1:35 PMreactormonk
09/02/2022, 1:35 PMreactormonk
09/02/2022, 1:37 PMsimon.vergauwen
09/02/2022, 1:38 PMreactormonk
09/02/2022, 1:39 PMreactormonk
09/02/2022, 1:39 PMsimon.vergauwen
09/02/2022, 1:40 PMsimon.vergauwen
09/02/2022, 1:42 PMsuspend
without losing any guarantees or patterns we are used to from working with IO
or other monads. This has also eliminated the need for monad transformers, which is quite significant on the JVM which doesn't like a lot of nesting, or capturing lambdas.reactormonk
09/02/2022, 1:49 PMsimon.vergauwen
09/02/2022, 1:53 PMsuspend
we also aim to eliminate the return types in the wrappers. Similar to effect systems so you can do things like.
suspend fun ResourceScope.myResource(): A =
autoCloseable { ConstructResource() }
Some design documents on this for Arrow 2.0:
Resource
Polymorphic effects