Saturday, December 19, 2009

Tuple progress

Made some terrific progress today on tuples. Here's the complete unit test so far:

import tart.reflect.Module;
import tart.testing.TestUtils;

@EntryPoint
def main(args:String[]) -> int {
  return TestUtils.runModuleTests(Module.thisModule());
}

def testTupleCreate() {
  var a = 1, 2;
  Debug.assertEq(1, a[0]);
  Debug.assertEq(2, a[1]);
}

def testTupleCreate2() {
  var a = 1, "Hello";
  Debug.assertEq(1, a[0]);
  Debug.assertEq("Hello", a[1]);
}

def testTupleCreateExplicitType() {
  var a:(int, int) = 1, 2;
  Debug.assertEq(1, a[0]);
  Debug.assertEq(2, a[1]);
}

def testTupleAssign() {
  var a = 1, 2;
  var b = a;
  Debug.assertEq(1, b[0]);
  Debug.assertEq(2, a[1]);
}

def testTupleReturn() {
  var a = returnTuple();
  Debug.assertEq(3, a[0]);
  Debug.assertEq("Hello", a[1]);
}

def returnTuple() -> (int, String) {
  return 3, "Hello";
}

Still to do:

-- Test passing tuples as parameters.
-- Test member-wise conversions.
-- Test tuples as members of structs / classes.
-- Write a compilation failure test to insure tuple immutability.
-- Implement unpacking assignment.

I'm really looking forward to being able to write:

   for key, value in map {
     // Do stuff
   }

As opposed to Java style:

   for (Map.Entry<KeyType, ValueType> entry : map) {
     KeyType key = entry.getKey();
     ValueType value = entry.getValue();
     // Do stuff
   }

The former is so much nicer in my opinioin.

No comments:

Post a Comment