Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option.flatten should return Option, not List #4474

Closed
scabug opened this issue Apr 12, 2011 · 7 comments
Closed

Option.flatten should return Option, not List #4474

scabug opened this issue Apr 12, 2011 · 7 comments
Assignees

Comments

@scabug
Copy link

scabug commented Apr 12, 2011

currently Option only has flatten via implicit conversion to Iterable, so:

scala> Some(Some(3)).flatten
res4: Iterable[Int] = List(3)

I was hoping to get Some(3) back, not List(3).

flatten (or join, I don't have an opinion on the right name) seems like an essential operation to have on monadic types like Option.

see also #661. it was closed as wontfix, but I think that's because Tony was suggesting adding lots of methods, not just this one.

@scabug
Copy link
Author

scabug commented Apr 12, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4474?orig=1
Reporter: @SethTisue

@scabug
Copy link
Author

scabug commented Apr 12, 2011

@paulp said:
This is in my large pile of "already done stuff I'm checking in after 2.9.0 ships" trying to lower the pace of change heading up to release.

@scabug scabug closed this as completed Jun 21, 2011
@scabug
Copy link
Author

scabug commented Jun 21, 2011

@paulp said:
Fixed in r24957.

@scabug
Copy link
Author

scabug commented Jun 21, 2011

@SethTisue said:
hooray!

@scabug
Copy link
Author

scabug commented Jul 2, 2011

gerferra said:
Is this expected behaviour?

Welcome to Scala version 2.10.0.r25217-b20110702020235 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val o = Some(Seq(1,2,3))
o: Some[Seq[Int]] = Some(List(1, 2, 3))

scala> o.flatten
<console>:9: error: Cannot prove that Seq[Int] <:< Option[B].
              o.flatten
                ^

"workaround":

scala> o.toList.flatten
res1: List[Int] = List(1, 2, 3)

@scabug
Copy link
Author

scabug commented Jul 2, 2011

@paulp said:
Pretty much expected. It's within the realm of possibility that someday it will, after realizing that the flatten on Option is ineligible, find the flatten on Iterable available after implicitly converting Option. But I wouldn't bet on it. Option is never going to fit seamlessly in with the collections, since it isn't one.

@scabug
Copy link
Author

scabug commented Jul 2, 2011

gerferra said:
Ok. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants