functional programming - inexplicable for-comprehension result in Scala -
this question has answer here:
i understand for-expression translated map , flatmap. found can not explain , need helps. here 2 toy examples:
for { none <- list(option(1),none) } yield 0 //res0: list[int] = list(0, 0)
q1: why some(1) map 0 ? expecting list(0),
update1:
comment of @marios , ths 1 more bizarre.
for(none <- list(some(1), none) ) yield none
returns list(some(1), none)
.
update2:
says variable, in ide, links none
object.
i use intellij auto-translate above for-expression map-expression:
list(option(1), none).map { case none => 0 } //scala.matcherror: some(1)
q2: error map-expression expected, while for-expression in first question doesn't give me error. why different computation ?
this appears bug in scala compiler. scala language specification states: "in first step, every generator p <- e
, p
not irrefutable type of e
replaced p <- e.withfilter { case p => true; case _ => false }
", , "a comprehension (p <- e) yield e′
translated e.map { case p => e′ }
."
this means example, for { none <- list(option(1), none) } yield 0
, should translated
list(option(1),none).withfilter { case none => true; case _ => false }.map{case none => 0}
which evaluates list(0)
, expected.
Comments
Post a Comment