kierans777
02/14/2022, 2:31 AMEither
eg: (Any) -> Either<A, B>
and I want Swift authors to be able to write functions that conform to that signature to be passed into functions in my library. That way I can delegate certain platform specific ideas to the platform.
However I can't seem to be able to create an Either
in Swift code. Looking in the MPP generated headers, I can see the Arrow_coreEither
type with its methods (eg: bimap
) but I can't find the Right
or Left
constructors. Is there something I have to do in my Kotlin project to expose the types properly? Has anyone done this successfully?Bart Kleijngeld
02/15/2022, 12:00 PMGopal S Akshintala
02/15/2022, 12:49 PMNonEmptyList<A>
is an awesome data-structure, I reckon one of it’s use-cases can be as a param to force callers to pass non-empty lists. But any reason why there is no NonEmptySet<A>
?Srki Rakic
02/15/2022, 5:02 PMsuspend
because it is making a network call (not pure), but I'm curious what would be the recommended approach to handling exceptions.noone
02/16/2022, 1:31 PMSecretX
02/18/2022, 11:14 PMfun test() = either<String, SomeObject> {
val someString = ""
// how can I return 'someString' as the left value of this either?
}
Gavin Ray
02/19/2022, 5:06 PMgiven/using
was one of the biggest things I really missed compared to coding in Scala and now Kotlin essentially has it 😅Sourabh Rawat
02/22/2022, 6:35 AMeffect
block.
I get something like
at foo.bar.BookService$bookReservation$suspendImpl$$inlined$invoke$1.invokeSuspend(Effect.kt:44) ~[main/:?]
at foo.bar.BookService$bookReservation$suspendImpl$$inlined$invoke$1.invoke(Effect.kt) ~[main/:?]
at foo.bar.BookService$bookReservation$suspendImpl$$inlined$invoke$1.invoke(Effect.kt) ~[main/:?]
at arrow.continuations.generic.SuspendMonadContinuation.startCoroutineUninterceptedOrReturn(SuspendingComputation.kt:89) ~[arrow-continuations-jvm-1.0.1.jar:?]
at arrow.continuations.Reset.suspended(Reset.kt:27) ~[arrow-continuations-jvm-1.0.1.jar:?]
at foo.bar.BookService.bookReservation$suspendImpl(BookService.kt:104) ~[main/:?]
Line number 104 does not even exist...
Edit: Same as https://github.com/arrow-kt/arrow/issues/2647gabib
02/23/2022, 10:54 AMjean
02/24/2022, 8:31 AM// create a hash from clear password
return hashPassword(password, pbkdf2Values)
// search for email/hash pair in db
.flatMap { database.findUserByCredentials(email, it) }
.map {
// if user is registered, check for existing auth token
when (val existingToken = database.token(it, userAgent)) {
is Either.Left -> createToken(jwtValues, it, userAgent) // no token -> create a new one
is Either.Right -> existingToken.value // token exists -> return it
}
}
I tried to use either.eager{}
or fold(ifLeft, ifRight)
but I didn’t find any solution more convincing 😕Norbi
02/24/2022, 10:26 AMcarbaj0
02/25/2022, 10:28 AMcarbaj0
02/27/2022, 6:37 PMcarbaj0
03/01/2022, 12:13 PMEffectScope<>
refactor available? I’m looking forward to trying it 😋Tower Guidev2
03/01/2022, 1:45 PMval work: Either<DatasourceException, *> = either { persistOutcome().bind() }
Cody Mikol
03/01/2022, 5:32 PMparZip
that just returns a TupleN
?Ties
03/02/2022, 2:36 PMrequire
being invariant, and pre/post conditions being something different.
fun increment(x: Int): Int {
pre(x > 0) { "value must be positive" }
return x + 1
}
fun increment2(x: Int): Int {
require(x > 0) { "value must be positive" }
return x + 1
}
But here both seem to work the same, and if I look into the code I see pre() is just calling require.
If I then look at the examples in the documentation (https://arrow-kt.io/docs/meta/analysis/types/)class
Positive(val value: Int) {
init { require(value > 0) }
}
and
class Positive(val value: Int) {
init {
pre(value > 0) { "value must be positive" }
post({ this.value > 0 }) { "value is positive" }
}
}
could the second example also be written
class Positive(val value: Int) {
init {
require(value > 0) { "value must be positive" }
post({ this.value > 0 }) { "value is positive" }
}
}
and it being the same thing? or is there a difference between pre and require?Stylianos Gakis
03/04/2022, 11:05 AMEither
to want to do something with the result, whether it’s left or right.
1. I see there’s one alternative of assigning the result to a value and doing a when
on it, which works but feels a bit “extra”.
2. I can do a tap
and a tapLeft
which i don’t like because I’m doing it in two functions.
3. There is also fold
which is what I am using the most, but this also comes with folding the two types into one which isn’t what I am looking to do in those scenarios.
I would like to be able to have an alternative that does basically what tap/tapLeft does but for both cases.
And now I wonder, does this not exist right now because:
1. It’s too easy to simply do our own implementation of it
2. No good name exists to denote such a behavior
3. It’s generally not a good idea and if I am looking to do something like this I probably want to do something else instead
And I feel like the 3rd option is what’s happening here so I wanted to bring this up here to see what people generally do in those cases 🤔Ties
03/07/2022, 4:22 PMPathVariable
@RestController
class MyController {
@JvmInline
value class Positive(val value: Int) {
init { require(value > 0) }
operator fun plus(other : Positive) = Positive(this.value + other.value)
}
@GetMapping("/{id}")
fun getTypeWithId(@PathVariable("id") id : Positive) : ResponseEntity<Positive>{
println(id)
return ResponseEntity.ok(id)
}
}
Turns out, if I send a request with the value -1
it will actually print and return -1
, so I just managed to create a Positive negative value 😄
So it compiles, and it seems that jackson is ignoring the require
when creating a value class. (I am aware that this is not Arrows fault, just wanted to share)simon.vergauwen
03/08/2022, 2:29 PMReplaceWith
? https://kotlinlang.slack.com/archives/C0B8H786P/p1646749747028839kierans777
03/09/2022, 12:52 AMList<Map<String, String>>
and I want to fold it down to Map<String, String>
. How could I do this using a Monoid in Arrow?than_
03/09/2022, 10:05 AMIterable::combineAll
looks to be the same as Iterable::fold
or am I missing something?Shalom Halbert
03/10/2022, 9:04 PMfun onSomethingChecked(isTrue: Boolean, onTrue: () -> Unit, onFalse: () -> Unit) {...}
Shalom Halbert
03/10/2022, 9:29 PMOption<Unit>
seems like a better option than Either
Marius Kotsbak
03/15/2022, 2:00 PMFlorian Magin
03/16/2022, 12:51 PMLens
as part of the Optics
component, but I don't know if this is the same idea, and because I am not familiar with those concepts already, I'd rather focus on learning and understanding the thing I actually need for my project, not something else that happens to have the same name 😅julian
03/17/2022, 7:11 PMrequire
is treated differently by the compiler compared with a project that doesn't use AA. Is this a correct observation?
For example, this, which doesn't explicitly use any AA features,
fun fn() {
val x = 1
require(x == 1)
}
results in a compiler error: could not parse predicate: x == 1
Whereas in a project without AA, there's no compiler error.Cody Mikol
03/17/2022, 9:12 PMOption<String?>
phldavies
03/18/2022, 12:24 PMfetch(key).flatMap { Either.catch({ Malformed(key) }) { f(it) } }
Youssef Shoaib [MOD]
03/19/2022, 6:15 PMcontext(A) fun <A> given(): A = this@A
sadly the type needs to be specified whenever given
is used because it doesn't infer the type parameter from usage (it instead latches onto the closest receiver).Youssef Shoaib [MOD]
03/19/2022, 6:15 PMcontext(A) fun <A> given(): A = this@A
sadly the type needs to be specified whenever given
is used because it doesn't infer the type parameter from usage (it instead latches onto the closest receiver).Gavin Ray
03/19/2022, 6:33 PMYoussef Shoaib [MOD]
03/19/2022, 6:39 PMcontext(List<Int>, List<String>, Long)
fun doSomething() {
repeat(given<Long>() {
println(given<List<Int>>().last())
println(given<List<String>>().first())
}
}
this@List
notation, which doesn't accomodate generics. This is inspired by given
which used to be a feature in Arrow Meta that did almost the same thingraulraja
03/19/2022, 6:52 PM@Context annotation class Config
@Context annotation class Persistence
@Config fun a(): A = ...
@Persistence fun b(): B = ...
context(A, B)
fun foo() {}
materialize<@Config A, @Persistence B>.foo()
That is DI for context receivers in addition to namespaced isolated injection based on type resolution. No source codegen just compiler substitutions in FIR and IR. If FIR works as expected IDEa will tell you when you are missing provider and other resolution errors when materializing.Gavin Ray
03/19/2022, 7:10 PMBecause the recommended way is to use theOhhh, got it that makes perfect sense. I've used Scala 3's `given`/`using` so I'm a bit familiar with the context receivers concept but I didn't know it had this limitation -- thanks for sharing!notation, which doesn't accomodate generics.this@List
That is DI for context receivers in addition to namespaced isolated injection based on type resolution. No source codegen just compiler substitutions in FIR and IR.That looks fantastic! One of the biggest uses I've had for
given
in Scala 3 was easy DI, since you can just declare singleton objects and then inject them with (using config: Config)
How do the annotations work here exactly?raulraja
03/19/2022, 9:45 PMinternal
making them of higher priority in candidate resolution