Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:15:43+00:00 2026-05-27T21:15:43+00:00

Is there any rational for Option not being Traversable ? In Scala 2.9, Seq(Set(1,3,2),Seq(4),Option(5)).flatten

  • 0

Is there any rational for Option not being Traversable?

In Scala 2.9, Seq(Set(1,3,2),Seq(4),Option(5)).flatten doesn’t compile and simply having it to implement the Traversable trait seams rational to me. If it’s not the case, there must be something I don’t see that don’t allow it. What is it?

PS: While trying to understand, I achieved awful things that compile, like:

scala> Seq(Set(1,3,2),Seq(4),Map("one"->1, 2->"two")).flatten
res1: Seq[Any] = List(1, 3, 2, 4, (one,1), (2,two))

PS2: I know I can write: Seq(Set(1,3,2),Seq(4),Option(5).toSeq).flatten or other ugly thing.

PS3: There seams to be work in the last month to make Option look more like Traversable without implementing it: commit, another commit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:15:44+00:00Added an answer on May 27, 2026 at 9:15 pm

    There may be challenges around having flatMap return an Option rather than a Traversable. Though that predates the whole 2.8 CanBuildFrom machinery.

    The question was asked once before on the mailing list but didn’t elicit a response.

    Here is an illustration:

    sealed trait OptionX[+A] extends Traversable[A] {
      def foreach[U](f: (A) => U): Unit = if (!isEmpty) f(get)
      def get: A
      def isDefined: Boolean
      def getOrElse[B >: A](default: => B): B
    }
    
    case class SomeX[+A](a: A) extends OptionX[A] {
      override def isEmpty = false
      def get = a
      def isDefined = true
      def getOrElse[B >: A](default: => B) = a
    }
    
    case object NoneX extends OptionX[Nothing] {
      override def isEmpty = true
      def get = sys.error("none")
      def isDefined = false
      def getOrElse[B](default: => B) = default
    }
    
    object O extends App {
      val s: OptionX[Int] = SomeX(1)
      val n: OptionX[Int] = NoneX
      s.foreach(i => println("some " + i))
      n.foreach(i => println("should not print " + i))
      println(s.map(_ + "!"))
    }
    

    The last line returns a List("1!") instead of Option. May be somebody can come up with a CanBuildFrom that would yield an SomeX("1!"). My attempt did not succeed:

    object OptionX {
      implicit def canBuildFrom[Elem] = new CanBuildFrom[Traversable[_], Elem, OptionX[Elem]] {
        def builder() = new Builder[Elem, OptionX[Elem]] {
          var current: OptionX[Elem] = NoneX
          def +=(elem: Elem): this.type = {
            if (current.isDefined) sys.error("already defined")
            else current = SomeX(elem)
            this
          }
          def clear() { current = NoneX }
          def result(): OptionX[Elem] = current
        }
        def apply() = builder()
        def apply(from: Traversable[_]) = builder()
      }
    }
    

    I need to pass the implicit explicitly:

    scala> import o._
    import o._
    
    scala> val s: OptionX[Int] = SomeX(1)
    s: o.OptionX[Int] = SomeX(1)
    
    scala> s.map(_+1)(OptionX.canBuildFrom[Int])
    res1: o.OptionX[Int] = SomeX(2)
    
    scala> s.map(_+1)
    res2: Traversable[Int] = List(2)
    

    Edit:

    So I was able to work around the issue and have SomeX(1).map(1+) return an OptionX by having OptionX extend TraversableLike[A, OptionX[A]] and overriding newBuilder.

    But then I get runtime errors on SomeX(1) ++ SomeX(2) or for (i <- SomeX(1); j <- List(1,2)) yield (i+j). So I don’t think it’s possible have option extend Traversable and do something sane in terms of returning the most specific type.

    Beyond feasibility, coding style wise, I’m not sure it’s a good thing to have Option behave like a Traversable in all circumstances. Option represent values that are not always defined, while Traversable defines methods for collections that can have multiple elements in it like drop(n), splitAt(n), take(n), ++. Although it would offer convenience if Option was also a Traversable, I think it may make intent less clear.

    Using a toSeq where necessary seems like a painless way to indicate that I want my option to behave like a Traversable. And for certain recurring use cases, there is the option2Iterable implicit conversion – so for instance this already works (they all return List(1,2)):

    • List(Option(1), Option(2), None).flatten
    • for (i <- List(0,1); j <- Some(1)) yield (i+j)
    • Some(1) ++ Some(2)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any rational reason, why native properties will not be part of Java
Is there any reason not to use the bitwise operators &, |, and ^
Is there an implementation of rational interval arithmetic in Python? This uses floats, not
Is there any way to check whether a file is locked without using a
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to
Is there any efficiency difference in an explicit vs implicit inner join? For example:
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Is there any known way of listing the WMI classes and their properties available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.