with scala 2.8.1, compiling this:
val t = (40, 2) println(for ((i, j) <- list(t)) yield + j) val e: either[string, (int, int)] = right(t) println(e.right.map { case (i, j) => + j }) println(for ((i, j) <- e.right) yield + j)
gives this:
test.scala:9: error: constructor cannot instantiated expected type; found : (t1, t2) required: either[nothing,(int, int)] println(for ((i, j) <- e.right) yield + j)
according programming in scala, expression should equivalent map/case expression, latter compiles. doing wrong, , how should this?
actually, not quite translation happening. may refer this answer more complete guide, case not explicitly mentioned there.
what happens comprehension pattern matching filters non-matching case. example,
for((i, j) <- list((1, 2), 3)) yield (i, j)
will return list((1, 2)): list[(any, any)]
, withfilter
called first. now, either
doesn't seem have withfilter
, use filter
, , here's actual translation of comprehension:
e.right.filter { case (i, j) => true; case _ => false }.map { case (i, j) => + j }
which gives same error. problem e.right
returns rightprojection
, filter
on rightprojection[a, b]
returns option[either[nothing, b]]
.
the reason there no such thing "empty" either
(or rightprojection
), needs encapsulate result on option
.
having said that, surprising when looked @ for-comprehension level. think right thing filter
return kind of filtered projection instead.
Comments
Post a Comment