summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sources/scala/CaseClass.scala6
-rw-r--r--sources/scala/Iterator.scala8
-rw-r--r--sources/scala/runtime/matching/Matcher.scala9
-rw-r--r--sources/scalac/typechecker/RefCheck.java4
-rw-r--r--test/files/run/misc.scala12
5 files changed, 20 insertions, 19 deletions
diff --git a/sources/scala/CaseClass.scala b/sources/scala/CaseClass.scala
index 34dbeeae1b..7268e44542 100644
--- a/sources/scala/CaseClass.scala
+++ b/sources/scala/CaseClass.scala
@@ -15,12 +15,12 @@ package scala;
*/
trait CaseClass {
- /** for a case class A(x1,...,xk), returns x_i for 1<= i <= k, null otherwise
+ /** for a case class A(x_0,...,x_(k-1)), returns x_i, null otherwise
*/
def selectElement(n:int):Any ;
- /** for a case class A(x1,...,xk), returns k
+ /** for a case class A(x_0,...,x_(k-1)), returns k
*/
- def numberOfElements(): int ;
+ def numberOfElements: int ;
}
diff --git a/sources/scala/Iterator.scala b/sources/scala/Iterator.scala
index f1e5540f02..b013888060 100644
--- a/sources/scala/Iterator.scala
+++ b/sources/scala/Iterator.scala
@@ -41,6 +41,14 @@ object Iterator {
def next = { val c = str charAt i; i = i + 1; c };
}
+ def fromCaseClass(n:CaseClass): Iterator[Any] = new Iterator[Any] {
+ private var c:Int = 0;
+ private val cmax = n.numberOfElements;
+ def hasNext = c < cmax;
+ def next = { val a = n.selectElement( c ); c = c + 1; a }
+
+ }
+
/** Create an iterator with elements
* <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code>
* where <code>e<sub>0</sub> = lo</code>
diff --git a/sources/scala/runtime/matching/Matcher.scala b/sources/scala/runtime/matching/Matcher.scala
index cb398a2bfc..140a1e034a 100644
--- a/sources/scala/runtime/matching/Matcher.scala
+++ b/sources/scala/runtime/matching/Matcher.scala
@@ -89,14 +89,7 @@ class Matcher( pgram:Grammar ) {
def getChildren( t:Any ):Iterator[Any] = t match {
//case n:scala.xml.Node => n.child;
- case n:CaseClass => new Iterator[Any] {
- var c:Int = 0;
- final val cmax = n.numberOfElements();
- def hasNext = c < cmax;
- def next = {
- c = c + 1; n.selectElement( c );
- }
- }
+ case n:CaseClass => Iterator.fromCaseClass( n );
case _ => Iterator.empty[Any];
}
// ------------------------------------------------------------------------
diff --git a/sources/scalac/typechecker/RefCheck.java b/sources/scalac/typechecker/RefCheck.java
index e5c6ffe89c..8cdb4788cf 100644
--- a/sources/scalac/typechecker/RefCheck.java
+++ b/sources/scalac/typechecker/RefCheck.java
@@ -699,7 +699,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds {
Tree body;
if( fields.length > 0) { // switch< n >
int tags[] = new int[ fields.length ];
- int i = 0; while( i < fields.length ) { tags[i] = ++i; };
+ int i = 0; while( i < fields.length ) { tags[i] = i; ++i; };
body = gen.Switch( gen.mkLocalRef( clazz.pos, seParam ),
tags,
fields,
@@ -714,7 +714,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds {
private Tree numberOfElementsMethod( ClassSymbol clazz ) {
Symbol seSym = clazz.newMethod( clazz.pos, OVERRIDE, Names.numberOfElements );
seSym.setInfo(
- Type.MethodType( Symbol.EMPTY_ARRAY, defs.INT_TYPE() ));
+ Type.PolyType( Symbol.EMPTY_ARRAY, defs.INT_TYPE() ));
clazz.info().members().enter( seSym );
Tree[] fields = caseFields( clazz );
return gen.DefDef(seSym, gen.mkIntLit( clazz.pos, fields.length ));
diff --git a/test/files/run/misc.scala b/test/files/run/misc.scala
index 4a16146c7f..1518efae1b 100644
--- a/test/files/run/misc.scala
+++ b/test/files/run/misc.scala
@@ -234,12 +234,12 @@ System.out.println();
case class Foo(i:int, j:char, c:Bar) ;
Console.println(
- Foo(3,'a',Bar()).selectElement( 0 ) == null
- && Foo(3,'a',Bar()).selectElement( 1 ) == 3
- && Foo(3,'a',Bar()).selectElement( 2 ) == 'a'
- && Foo(3,'a',Bar()).selectElement( 3 ) == Bar()
- && Foo(3,'a',Bar()).selectElement( 4 ) == null
- && Bar().numberOfElements() == 0
+ Foo(3,'a',Bar()).selectElement( -1 ) == null
+ && Foo(3,'a',Bar()).selectElement( 0 ) == 3
+ && Foo(3,'a',Bar()).selectElement( 1 ) == 'a'
+ && Foo(3,'a',Bar()).selectElement( 2 ) == Bar()
+ && Foo(3,'a',Bar()).selectElement( 3 ) == null
+ && Bar().numberOfElements == 0
&& Foo(3,'a',Bar()).numberOfElements() == 3);
//############################################################################