asRegex.get
:.asUnsafeRegex
, which is essentially the same thing but makes it clearer you're doing something unsaferx"..."
syntax, much safer since regular expressions are validated at compile time?rx"..."
syntax for such cases, it's much better than .r.pattern
implicitly[GroupDecoder[A]]
by GroupDecoder[A]
, both more idiomatic and fasterobject KantanRegexMatchNullPointer {
val regex = rx"([0-9|-]+)".asUnsafeRegex[CCDT]
implicit val timeDecoder: GroupDecoder[LocalDate] = {
GroupDecoder[String].mapResult(s =>
DecodeResult(LocalDate.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd")))
)
}
def main(args: Array[String]): Unit = {
"2016-07-08".evalRegex(regex)
}
}
case class CCDT(dt: LocalDate)
kantan.regex
related, but still.
implicitly[GroupDecoder[CC4]]
was never going to work. You can have a MatchDecoder[CC4]
, but not a GroupDecoder
. Do you need me to explain why?
MatchDecoder
decodes a match - potentially many strings
([a-z]+) ([0-9|-]+)
has two groups: the first one, at least one letter, and the second one, at least one digit
(String, Int)
GroupDecoder[A]
is essentially a String => A
where a MatchDecoder[A]
is a Seq[String] => A
GroupDecoder
for both A
and B
in order to derive a MatchDecoder[(A, B)]
. Since CC4
cannot have a GroupDecoder
, you cannot get a MatchDecoder[(CC4, B)]
for any B
yep that part I get
but conceptually speaking there is not much difference in my eyes between something like ((A, B), (C, D)) and (A, B, C, D).
so that's why I would think that .evalRegex[((A, B), (C, D)) could work just as .evalRegex[(A, B, C, D)] works
but you don't have to answer this immediately