Hi all, another new comer here. I'm reading the do...
# arrow
m
Hi all, another new comer here. I'm reading the docs, and going through Higher Kinds and Typeclasses, and trying to use some of the examples. The multiplyBy2 example I followed fine:
Copy code
val o = Option.functor().multiplyBy2(Option(1))
assertThat(o.fix()).isEqualTo(Option(2))
But I've just come across this in the dependency injection notes
Copy code
fun <F> Show<Kind<F, Int>>.printAllValues(fa: List<Kind<F, Int>>): Unit {
  fa.forEach { println(fa.show()) }
}
I'm trying to write some code to use this, but I'm having trouble finding the right form to invoke it. How would I call it with (say) a list of Option<Int> values? Thanks!
r
In the current encoding which is gonna change all this boilerplate it would currently look like:
Copy code
import arrow.Kind
import arrow.core.extensions.show

fun List<Int>.printAllValues(): Unit {
  Int.show().run {
    forEach { println(it.show()) }
  }
}

fun main(args: Array<String>) {
    listOf(1, 2, 3).printAllValues()
}
Since you are using Int you can use
Int.show
directly otherwise you would add
Show<A>
as argument to the function if it was
A
instead of
Int
m
Thanks Raul. How would I do this with the given function from the docs, using a list of Options, e.g.
listOf(Option(1), Option(2))
- I'm specifically trying to understand how to use the given printAllValues as an extension function of
Show<Kind<F, Int>>
so I can work out how to pass a value in to it
s
If you want to print
Some(1)
and
None
then you could simply adjust the example the way Raul suggested.
Copy code
import arrow.Kind
import arrow.core.extensions.show

fun <A> List<A>.printAllValues(show: Show<A>): Unit {
  show.run {
    forEach { println(it.show()) }
  }
}

fun main(args: Array<String>) {
    listOf(1, 2, 3).printAllValues(Int.show()
    listOf(Some(1), None, Some(3)).printAllValues(Option.show(Int.show))
}
m
Thanks Simon, but without defining any new printAllValues function, I want to try and invoke the one from the docs (https://arrow-kt.io/docs/patterns/dependency_injection/). I just don't understand how to though. I feel I'd understand that section better if I can write a concrete example for it, like I have for the multiplyBy2 example.
s
I’m not sure what you mean? What I did was refactor Raul’s function to work with
A
instead of
Int
, so I didn’t really write a new function but I made his example function more generic instead.
So instead of only working with
Int
it now also works with
Option<Int>
,
String
or anything else you can provide a
Show
instance for.
m
I'm trying to use this from the docs:
Copy code
fun <F> Show<Kind<F, Int>>.printAllValues(fa: List<Kind<F, Int>>): Unit {
  fa.forEach { println(fa.show()) }
}
but don't know how to call it, or use it. I was after an example using a list of Options
s
Ah okay.
Copy code
Option.show(Int.show()).printAllValues(emptyList())
m
message has been deleted
s
😱 Bit sad to see the kotlin inferrer trip there
Oh, the
Option.show
is not parameterized to take a
Show<A>
instance but uses
toString()
instead.
m
I wonder if it's the inner error tripping
s
Option.show().printAllValues(items)
m
still not working, same error. I'm on kotlin 1.3.60. Is it working for you?
s
I quickly send on my own machine.
OptionShow<A>
should take a
Show<A>
instead so can customize the printing of
A
. If you’re interested in contributing I could help you make the fix 🙂
Sec, I’ll send a snippet with imports
m
thanks Simon.
s
I had to update the
printAllValues
function because
Show
for option is not defined for the
HKT
but only for the concrete type.
Show<Option<A>>
instead of
Show<OptionOf<A>>
. Same offer applies if you want to contribute I’d love to help 😉
With the following
Show
definition for
Option
your example should also work.
Copy code
fun <A> Option.Companion.show2(): Show<OptionOf<A>> =
  Show.invoke { fix().toString() }
Note how it’s defined for
OptionOf<A>
instead of
Option<A>
.
Sorry for the confusion. These are the kind of quirks we’re solving with Arrow Meta for the 1.0 release 🙂
m
Are there any Show types that would work with the original documented function? I tried Either too and am still struggling to get anything to work with it.
s
I would have to check but I am assuming not, these were all added at once by the same contributor IIRC. This typically is also not a common occurrence, and if I saw the above function in a code review I would comment there that it should be refactored to
A
instead of
Kind<F, Int>
anyway.
🔝 1
m
I also couldn't understand how the documented implementation worked either by reading it, which is why I was keen to get an example that works. It loops over the list, calling show on the list, instead of the items of the list. It should have been
println(it.show())
from what I can tell.
s
That is correct!
m
btw, thanks for the offer to assist in contributing, but i'm still way over my head to feel I'd contributed anything meaningful, as I'm still trying to understand it.
s
You’re welcome! All contributions are welcome and helpful! No matter how big or small. It’s hard maintaining so much documentation.
r
Without inductive resolution of type class constrains al wrappers are awkward to use. With the work we are doing in type proofs you will just get the syntax without imports if there is proof of the extension and extensions will be derived so here you don’t need to mess with what’s inside the Option
That’s part of the Arrow Meta changes
Something like this: