The only structural type used in Scala library

67 views
Skip to first unread message

Grzegorz Kossakowski

unread,
Jul 7, 2011, 9:14:13 AM7/7/11
to scala-i...@googlegroups.com
Hi,

I just made a discovery that in trunk's Scala library there's one use of structural type. It's being used in implementation of Iterator.span (Iterator.scala:508).

I'm pretty sure that structural type in that place is not intended and is there only because type inference inferred structural type. Patch below fixes that problem. Would be great if someone could commit it. I can create a ticket if you prefer.

Also, I could send a pull request to Paul but I don't know if this is supported.

diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index c9f7500..afcd28f 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -507,7 +507,14 @@ trait Iterator[+A] extends TraversableOnce[A] {
    */
   def span(p: A => Boolean): (Iterator[A], Iterator[A]) = {
     val self = buffered
-    val leading = new Iterator[A] {
+
+    /**
+     * Giving a name to this iterator (as opposed to trailing) because
+     * anonymous class is represented as a structural type that trailing
+     * iterator is referring (the finish() method) and thus triggering
+     * handling of structural calls. It's not what's intended here.
+     */
+    class Leading extends Iterator[A] {
       private var isDone = false
       val lookahead = new mutable.Queue[A]
       def advance() = {
@@ -528,6 +535,7 @@ trait Iterator[+A] extends TraversableOnce[A] {
         lookahead.dequeue()
       }
     }
+    val leading = new Leading
     val trailing = new Iterator[A] {
       private lazy val it = {
         leading.finish()


--
Grzegorz Kossakowski

Grzegorz Kossakowski

unread,
Jul 12, 2011, 7:30:40 AM7/12/11
to scala-i...@googlegroups.com
On 7 July 2011 15:14, Grzegorz Kossakowski <grzegorz.k...@gmail.com> wrote:
Hi,

I just made a discovery that in trunk's Scala library there's one use of structural type. It's being used in implementation of Iterator.span (Iterator.scala:508).

I'm pretty sure that structural type in that place is not intended and is there only because type inference inferred structural type. Patch below fixes that problem. Would be great if someone could commit it. I can create a ticket if you prefer.

Created a ticket so it doesn't get forgotten:


--
Grzegorz Kossakowski
Reply all
Reply to author
Forward
0 new messages