Sourabh Rawat
12/16/2021, 7:09 PMeither
block from which I want to return a Left
value without any bind
calls. For example
either<Error, Response> {
when(foo) {
1 -> callFunReturningEither().bind()
else -> Error("foo is in else") <-- How to return this Left value?
}
}
I have tried Error("foo is in else").left().bind()
but it gives Returning type parameter has been inferred to Nothing implicitly. Please specify type arguments explicitly to hide this warning. Nothing can produce an exception at runtime.
which I think is expected.pakoito
12/24/2021, 2:33 AMwakingrufus
01/25/2022, 10:24 PMJerry Liu
03/25/2022, 3:37 PMdp
04/14/2022, 9:12 PMMuhammad Talha
04/28/2022, 7:22 AMOutcome.Success
class with a value. However is it possible to use it without a value? There are some cases where I don’t want to pass a value in. I’m not quite sure what the definition would be for me not to pass a value in while not making value optional. Thanks!
sealed class Outcome<out T: Any> {
class Success<out T: Any>(val value: T) : Outcome<T>()
class Error(val msg: String, val exp: Exception? = null) : Outcome<Nothing>()
}
Uberto Barbini
06/30/2022, 6:55 AMimashnake_
07/09/2022, 8:22 AMreactormonk
07/14/2022, 12:55 PMFlow
?Rohde Fischer
07/31/2022, 1:59 PMEither
I sometimes run into having multiple results where I need the results for another call. There I'm usually end up in an ugly structure with nested `flatMap`s. E.g.:
val result1 = foo() // : Either<Error, Result1>
val result2 = foo() // : Either<Error, Result2>
val result3 = foo() // : Either<Error, Result3>
val transformed = result1.flatMap { res1 ->
result2.flatMap { res2 ->
result3.flatMap { res3 -> someCall(res1, res2, res3) }
}
}
isn't there a nicer way to do this?Muhammad Talha
08/16/2022, 4:59 AMRicky Clarkson [G]
08/16/2022, 6:18 AMMuhammad Talha
08/16/2022, 6:29 AMsimon.vergauwen
08/16/2022, 7:13 AMreactormonk
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?Nathan Bedell
09/28/2022, 9:01 PMTies
10/17/2022, 6:35 PMreactormonk
10/21/2022, 12:53 PMwhen
statement?
Aka
when (Pair(x, y)) {
Pair(true, false) -> ...
}
reactormonk
10/28/2022, 11:41 AMPair
match?
val mimes = rec.mimeType.split("/")
when (mimes[0] to mimes[1]) {
"text" to "vcard" -> {}
"video" to _ -> {}
}
^ the second one isn't really working out.Kristian Nedrevold
11/09/2022, 8:24 PMstruct Circle {
radius: u32,
}
impl Circle {
fn new(radius: u32) -> Circle {
Circle { radius }
}
}
trait Shape {
fn area(&self) -> f64;
fn perimeter(&self) -> f64;
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * (self.radius as f64).powi(2)
}
fn perimeter(&self) -> f64 {
2.0 * std::f64::consts::PI * (self.radius as f64)
}
}
fn pretty_print<T: Shape>(shape: T) {
println!("Area: {}", shape.area());
println!("Perimeter: {}", shape.perimeter());
}
fn main() {
let circle = Circle::new(1);
pretty_print(circle);
}
So the trait is like a Kotlin Interface. And now my function pretty_print can take any type(struct) that implements Shape. This seems like a very powerful feature to me as it means I can extend types. How can I achieve the same thing in Kotlin?gsala
12/02/2022, 8:34 AMfun ((A, B) -> Unit).whatShouldThisBeNamed(b: B): (A) -> Unit {
return { a -> this(a, b) }
}
(A function that returns a simpler function by providing one of the parameters to the first function)Szymon Sasin
12/05/2022, 7:02 PMval s = sequenceOf("Lorem", "ipsum", "dolor", "sit", "amet")
What’s the best way to limit elements so total size is smaller than desired.
For example:
val s2 = s.someFunction(maxSize = 17) { it.length }
println(s2.toList()) // [Lorem, ipsum, dolor]
Marius Kotsbak
12/08/2022, 12:09 PMinterface ServiceLocator<I> {
// fun getRealImpl(): I
fun getEmulatedImpl(): I
// fun getStubbedImpl(): I
}
context(ServiceLocator<I>)
inline fun <reified I> locateEmulated(): I = getEmulatedImpl()
object StringServiceLocator : ServiceLocator<String> {
override fun getEmulatedImpl(): String = "test"
}
object LongServiceLocator : ServiceLocator<Long> {
override fun getEmulatedImpl(): Long = 5
}
object StringLongLocatorFactory: ServiceLocatorFactory {
// In the future (scope properties): with val ...
inline fun <reified I> locate(): I {
with(LongServiceLocator) {
with(StringServiceLocator) {
return locateEmulated<I>()
}
}
}
}
No required context receiver found: Cxt { context(ServiceLocator<I>) public inline fun <reified I> locateEmulated(): I defined in [...] in file ServiceLocatorFactory.kt[SimpleFunctionDescriptorImpl@1aa4cbc9] }Kristian Nedrevold
12/27/2022, 2:22 PMfun <A, B> memoize (f: (A) -> B) = { a: A ->
val cache = mutableMapOf<A, B>()
cache.computeIfAbsent(a) { f(a) }
}
reactormonk
12/29/2022, 2:00 PMjulian
01/05/2023, 6:14 PMBreaker ACT
02/01/2023, 3:00 AMPiotr Krzemiński
02/08/2023, 7:51 AMvar
or for
loops (mutability in general), with a way to opt out in certain cases. Think: a step towards more pure FP. I’m wondering if you’d be keen to use it and, if yes, what approach you’d suggest to implement it. I’m thinking about a detekt rule or a compiler plugin that would emit an error or a warning (configurable)Teimatini Marin
02/10/2023, 4:39 PMfun main() = runBlocking {
var lastline = ""
readHugeFile().collect { value ->
val lines = (lastline+value).lines()
lines.dropLast(1).forEach {
println(it)
}
lastline = lines.last()
}
println(lastline)
}
fun readHugeFile(): Flow<String> = flowOf(
"""line 1
|line 2
|li""".trimMargin(),
"""ne 3
|line 4
|lin""".trimMargin(),
"""e 5
|line 6
""".trimMargin()
)
dave08
02/13/2023, 4:50 PMdave08
02/13/2023, 4:50 PMraulraja
02/13/2023, 5:11 PMsimon.vergauwen
02/13/2023, 5:42 PM0.x seriesTo put some more perspective, Arrow 1.x was release almost 2021 and Arrow 2.x is being released later this year. Within 1.x.x there were no breaking changes, and the last version of 1.x.x will be source compatible with 2.x.x. 1.x.x -> 2.x.x will remove some obscure APIs, and further clean-up some old legacy things that are not being used.
dave08
02/13/2023, 6:27 PMsimon.vergauwen
02/13/2023, 6:30 PMvery minimal changesThis can be done in a binary compatible way, so we might just stick to 2.x.x even when context receivers land into the language.
1.1.6-alpha.26
and check the PRs for all remaining work that is still to be merged before the 1.2.x.
release ☺️dave08
02/20/2023, 2:58 PMsimon.vergauwen
02/20/2023, 3:29 PMis stable, but is it production-ready?Yes, it's production-ready.
Also, even if not, maybe it would be possible to have a release with a bunch of typealiases with the new names to avoid new projects having to migrate?This is already the case, current
1.1.6-alpha.x
is what will be released as 1.2.x
in 2-3 weeks and all non-deprecated code will be source and binary-compatible with 2.x.x
. 1.2.x
is 1.1.x
+ everything from 2.x.x
back-ported. @Deprecated
if it will be removed from 1.x.x
-> 2.x.x
and all non-deprecated code will remain unchanged.
We're still figuring out how to deprecate Validated
nicely, and plan a migration plan. This can potentially be partially be automated as well. We want everything to be as automatic, and as painless as possible.