Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-5793

definition inside a for comprehension based on an Either fails to compile

    Details

    • Type: Bug
    • Status: CLOSED
    • Priority: Major
    • Resolution: Not a Bug
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None
    • Environment:

      scala 2.9.2 and 2.10.0-M3

      Description

      Including a definition in a for comprehension works if the comprehension is using Option, but the compiler complains about a for comprehension with Either (RightProjection).

      Using for comprehensions makes for very readable code, and flowing though errors via Either seems like it ought to be the right thing to do.

      Example:

      def opt:Option[Char] = Some('e')
      def f(c:Char):String = "ha"
      def either:Either[Int,Char] = Right('r')
       
      val optComprehensionWithAssignment:Option[Int] =
        for {
          c <- opt
          x = c.toInt
        } yield x
       
      val eitherComprehensionNoAssignment:Either[Int,String] = 
        for { 
          c <- either.right
        } yield {
          val s = f(c)
          s
        }
       
      val eitherComprehensionWithAssighment:Either[Int,String] = 
        for { 
          c <- either.right
          s = f(c)                // doesn't compile
        } yield s
       
       
      val workaround:Either[Int,String] = 
        for { 
          c <- either.right
          s <- Right(f(c)).right       // workaround's a bit awkward
        } yield s
      
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mighdoll Lee Mighdoll
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: