twitter / bijection

Reversible conversions between types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"invert" of as[T]

ShahOdin opened this issue · comments

so with the help of the as method, we can do:

val digit = 123
def addZeros(s: String) = s + "000"
addZeros(digit.as[String])  //123000

i want to also be able to do:

val text = "123"
def double(i: Int) = 2 * i
double(text.as[Int]) // 246

but I can't. Why is the generalised as not provided? is this because of a choice or a limitation?

ok. silly of me. the conversion between strings and ints is injection not bijection. hence why the inverse does not work! the as method works both ways for a bijection.

You can do ”123”.as[Try[Int]] or Option[Int] and it should work.