[package] [Java implementation] [Execution output]


HigherKindedTypes


import java.util.Arrays;
import java.util.List;

import church.util.Lists;
import church.lang.Array;

/*
 * Church's "a[i]" syntax is syntactic sugar for the ordinary method call: "get(a, i)".
 * In addition, the get(a, i) method call is defined as a generic function in church.util.Collections
 * (see its definition therein).
 * For arrays, the implementation of "get(a, i)" is provided by the church.lang.Array import.
 * For instances of java.util.List, define "get(a, i)" as the instance method defined in the Java collections framework
 * (which also happens to use the name "get" for element access).
 */
(List: a)[i] = a.get(i);

/** This method will work on all types of collection, not just Java arrays. */
check(a) = {
    s = a[0] + a[1] + a[2];
    output << "Sum is: " << s << "\n";
    assert s == 6;
}

/**
 * The above methods "check" and "get" are both abstractions involving a 'higher-kinded' type.
 * Call check() with two different container types: a regular Java Array and an instance of java.util.List.
 */
void: main(String[]: args) = {
    a = [0, 2, 4];
    check(a);

    l = Arrays.asList(a);
    check(l);
}