Skip to content

Commit 974fa52

Browse files
committed
HList#cons static factory method auto-promotes
1 parent ec22f1a commit 974fa52

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/main/java/com/jnape/palatable/lambda/adt/hlist/HList.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

3+
import com.jnape.palatable.lambda.functions.builtin.fn1.Downcast;
4+
35
import java.util.Objects;
46

57
/**
@@ -63,7 +65,7 @@ public static HNil nil() {
6365
* @return the newly created HList
6466
*/
6567
public static <Head, Tail extends HList> HCons<Head, Tail> cons(Head head, Tail tail) {
66-
return new HCons<>(head, tail);
68+
return Downcast.<HCons<Head, Tail>, HCons<Head, ? extends HList>>downcast(tail.cons(head));
6769
}
6870

6971
/**
@@ -271,7 +273,7 @@ public final boolean equals(Object other) {
271273
if (other instanceof HCons) {
272274
HCons<?, ?> that = (HCons<?, ?>) other;
273275
return this.head.equals(that.head)
274-
&& this.tail.equals(that.tail);
276+
&& this.tail.equals(that.tail);
275277
}
276278
return false;
277279
}

src/test/java/com/jnape/palatable/lambda/adt/hlist/HListTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
import org.junit.Test;
44

5-
import static com.jnape.palatable.lambda.adt.hlist.HList.cons;
6-
import static com.jnape.palatable.lambda.adt.hlist.HList.nil;
7-
import static com.jnape.palatable.lambda.adt.hlist.HList.singletonHList;
8-
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertFalse;
11-
import static org.junit.Assert.assertNotEquals;
12-
import static org.junit.Assert.assertSame;
13-
import static org.junit.Assert.assertTrue;
5+
import static com.jnape.palatable.lambda.adt.hlist.HList.*;
6+
import static org.hamcrest.CoreMatchers.instanceOf;
7+
import static org.junit.Assert.*;
148

159
public class HListTest {
1610

@@ -36,6 +30,18 @@ public void convenienceStaticFactoryMethods() {
3630
assertEquals(nil().cons(false).cons(4.0).cons("3").cons('2').cons(1), tuple(1, '2', "3", 4.0, false));
3731
}
3832

33+
@Test
34+
public void autoPromotion() {
35+
assertThat(cons(1, nil()), instanceOf(SingletonHList.class));
36+
assertThat(cons(1, singletonHList(1)), instanceOf(Tuple2.class));
37+
assertThat(cons(1, tuple(1, 1)), instanceOf(Tuple3.class));
38+
assertThat(cons(1, tuple(1, 1, 1)), instanceOf(Tuple4.class));
39+
assertThat(cons(1, tuple(1, 1, 1, 1)), instanceOf(Tuple5.class));
40+
assertThat(cons(1, tuple(1, 1, 1, 1, 1)), instanceOf(Tuple6.class));
41+
assertThat(cons(1, tuple(1, 1, 1, 1, 1, 1)), instanceOf(Tuple7.class));
42+
assertThat(cons(1, tuple(1, 1, 1, 1, 1, 1, 1)), instanceOf(Tuple8.class));
43+
}
44+
3945
@Test
4046
public void nilReusesInstance() {
4147
assertSame(nil(), nil());

0 commit comments

Comments
 (0)