Collection operators

91 views
Skip to first unread message

Michael Schmitz

unread,
May 20, 2013, 7:35:00 PM5/20/13
to scala-user
I know collection operators have been discussed at length, but this is an unfortunate problem in a "typesafe" language.

scala> Set("foo") ++ Set("bar") + "baz"
res2: scala.collection.immutable.Set[java.lang.String] = Set(foo, bar, baz)

scala> Seq("foo") ++ Seq("bar") + "baz"
res3: java.lang.String = List(foo, bar)baz

Of course, it's even worse when you have variables and the collection type is not explicit on this line.

Peace.  Michael

Rex Kerr

unread,
May 20, 2013, 7:38:42 PM5/20/13
to Michael Schmitz, scala-user
any2stringadd strikes again!  Fortunately, you can shadow the name to remove it.

scala> implicit def any2stringadd(x: Option[Unit]) = x
any2stringadd: (x: Option[Unit])Option[Unit]


scala> Seq("foo") ++ Seq("bar") + "baz"
<console>:12: error: value + is not a member of Seq[String]

              Seq("foo") ++ Seq("bar") + "baz"

I hope someday any2stringadd it will go away, but it is a major breaking change.

  --Rex


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Schmitz

unread,
May 20, 2013, 7:40:55 PM5/20/13
to Rex Kerr, scala-user
Nice trick!

Simon Ochsenreither

unread,
May 20, 2013, 7:54:24 PM5/20/13
to scala...@googlegroups.com, Michael Schmitz
With string interpolation shipping since 2.10, I think it would make sense to finally try and get any2stringadd deprecated for 2.12.

Som Snytt

unread,
May 20, 2013, 9:31:22 PM5/20/13
to Simon Ochsenreither, scala-user, Michael Schmitz
Today I had occasion to quote:

"Never underestimate how long are the tendrils of any2stringadd!"

http://stackoverflow.com/a/12150383/1296806


I thought I noticed at the same time:

> @deprecated("Use String interpolation", "2.11.0") def any2stringadd(x: Any): StringAdd[Any]





On Mon, May 20, 2013 at 4:54 PM, Simon Ochsenreither <simon.och...@gmail.com> wrote:
With string interpolation shipping since 2.10, I think it would make sense to finally try and get any2stringadd deprecated for 2.12.

Som Snytt

unread,
May 20, 2013, 9:33:57 PM5/20/13
to Simon Ochsenreither, scala-user, Michael Schmitz
Shall I add, both


> "Never underestimate how long are the tendrils of any2stringadd!"

and

> "Use String interpolation"

should have their own Scala-wear t-shirts.

Maybe it could say "Use String interpolation" on the back, and on the front something like:

"I'm with $dummy!"

Simon Ochsenreither

unread,
May 21, 2013, 5:25:54 AM5/21/13
to scala...@googlegroups.com, Simon Ochsenreither, Michael Schmitz
any2stringadd is now an implicit class, that's why the methods is deprecated.
Reply all
Reply to author
Forward
0 new messages