September 30, 2008

Chapter: Generics

Create and test a SixTuple generic.


In Java:

package chapter.generics;

class SixTuple<A, B, C, D, E, F> {
public final A a;
public final B b;
public final C c;
public final D d;
public final E e;
public final F f;

public SixTuple(A a, B b, C c, D d, E e, F f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
}

public class Exercise3 {

static SixTuple<Integer, String, Double, Boolean, String, Integer> foo() {
return new SixTuple<Integer, String, Double, Boolean, String, Integer>(
5, "hi", 33.3, true, "bye", 100);
}

public static void main(String[] args) {
SixTuple<Integer, String, Double, Boolean, String, Integer> x = foo();
System.out.println(x.b);
System.out.println(x.a);
System.out.println(x.e);

}

}


Lisp can return values in many natural and easy ways, including lists and the values function. Since a lisp programmer would probably avoid creating a special tuple class just to return multiple values, here is an alternative and simple way to accomplish the feat:
(defun foo ()
(values 5 "hi" 33.3 t "bye" 100))

(multiple-value-bind (a b c d e f) (foo)
(print b)
(print a)
(print e))

0 comments: