Repository navigation
Description
I'm trying to get through the Java track and I'm really confused about two things to do with the simple-linked-list exercise.
- Is there a reason this exercise doesn't make use of polymorphism?
- How can I possibly write even the signature of a method that will make the last two test conditions succeed?
Allow me to explain the second confusion, here are the method signatures, followed by the compiler errors or failed tests in each case:
Polymorphism:
public <T> T[] asArray(Class<T> cls)> error: generic array creation T[] answer = new T[size()];Primitive array:
public int[] asArray(Class<?> cls)> error: method containsExactly in class AbstractIntArrayAssert<S> cannot be applied to given types;
> assertThat(list.asArray(Object.class)).containsExactly(expected);
> ^
> required: int[]
> found: Object[]
> reason: varargs mismatch; Object[] cannot be converted to int
> where S is a type-variable:
> S extends AbstractIntArrayAssert<S> declared in class AbstractIntArrayAssertInteger array:
public Integer[] asArray(Class<?> cls)> error: no suitable method found for containsExactly(Object[])
> assertThat(list.asArray(Object.class)).containsExactly(expected);
> ^
> method ObjectEnumerableAssert.containsExactly(Integer...) is not applicable
> (varargs mismatch; Object[] cannot be converted to Integer)
> method AbstractObjectArrayAssert.containsExactly(Integer...) is not applicable
> (varargs mismatch; Object[] cannot be converted to Integer)
> where CAP#1 is a fresh type-variable:
> CAP#1 extends AbstractObjectArrayAssert<CAP#1,Integer> from capture of ?Object array:
public Object[] asArray(Class<?> cls)> SimpleLinkedListTest > canReturnListAsArray FAILED
> java.lang.AssertionError at SimpleLinkedListTest.java:63As far as I can tell the last three errors are solely because of a type mismatch and inability for Java and/or the test suite to cast arrays in the obvious way. The first error, as I understand it, is a limitation of Java's polymorphism (even though I have no clue how I'm supposed to implement java.util.Collection<E>.toArray(T[]) correctly without creating a generic array).
I'm guessing that I'm missing something which is either obvious or an idiosyncrasy of Java. Can someone please help?
Activity
matthewmorgan commentedon Mar 29, 2016
Hi,
I will take a look and try to help. With a little luck I will have time to answer tonight.
Thanks
matthewmorgan commentedon Mar 29, 2016
I think the issue here is that you should be using generics, not casting. If you post your code I could take a look and offer more specific feedback.
Otherwise, you should feel free to submit a partially working (or not at all working!) solution to exercism and look around at what others have done.
I don't think there's a fault with the test suite as it is.
Jonah-Horowitz commentedon Mar 29, 2016
I haven't cast anything, the testing framework, when testing equality of contents of an
Integer[]and anObject[]seems to be trying to cast theObject[]as anInteger[], then throwing a compile-time exception when it doesn't manage to do that. Notice that errors 2 and 3 above reference castings that take place within thecontainsExactlymethod's call.Also the test suite does not seem to be set up to support generics, or as I have been calling the use of generics polymorphism, which is why I asked my first question - I would have expected the test suite to require the class to be instantiated as e.g.
new SimpleLinkedList<Integer>()rather thannew SimpleLinkedList()if generics were intended to be used.Checking the solutions already submitted, there are 6 other than mine at this time. One seems to have simply returned an
Object[](which I tried, and it didn't work for me) and every other solution usesArray.newInstance(class, size)which it then casts as an array of genericsT[](orU[]in one case).My incomplete solution is located here, if you feel like taking a look.
I guess my problem here is that I try to avoid using reflection when possible (primarily because of the reduced performance), so I didn't look in
Array(or inArrays, where I might naively have expected it to be) for creating an array of a generic type.Thank you for your help thus far.
matthewmorgan commentedon Mar 29, 2016
I'm happy to help. I'll give it a closer look and see what I can figure out. 😄
matthewmorgan commentedon Apr 1, 2016
@Jonah-Horowitz I see that you are not using casting. I think I might not be understanding your question exactly, so bear with me:
The example solution that I wrote for this exercise does use casting and reflection in the last method that returns the array. In this case the cast is safe because we know the types will match by design. You can see the code on my submission-- it's the same as what I built here to run the test suite against.
I certainly don't consider myself a Java guru, but I'm a bit confused about your question about polymorphism-- are you suggesting that the test suite should have multiple overridden methods for testing different classes of inputs? That's exactly what we avoid by using generics-- we get to pass the class as a parameter. In fact, your example earlier:
is using generics. That's what
<T>is doing for you-- passing the class as a param.The test suite uses integers, but if you build your solution using generics, you could for example pass the following test:
It's not hard to imagine building a "real" Simple Linked List implementation and wanting it to be flexible enough to store any type of object.
I don't know if that makes sense or if I'm addressing your concerns-- please let me know. If you have an alternative implementation that would accept any class of object for storage, and that implementation doesn't use generics, I would love to have a look at it for my own edification.
Anyway, if I can't help you maybe I can lean on @jtigger or @sit to help.
Cheers!
Jonah-Horowitz commentedon Apr 1, 2016
I'm quite aware of the usefulness of generics, what I'm asking in question 1 is why isn't this exercise encouraging its use more? In other words, why isn't the class
SimpleLinkedListparameterized rather than only the methodtoArray? Essentially, why isn't the simple-linked-list exercise more like the linked-list exercise with respect to generics?If you like, I can alter my simple-linked-list solution to use generics in the way I intend and post it here later today, but it would likely not actually compile as a solution to the provided test suite.
Edit: here's my altered answer (I haven't compiled it so there may be some errors).
matthewmorgan commentedon Apr 1, 2016
That would be great -- it sounds like we might have the beginnings of a PR
:)
On Fri, Apr 1, 2016 at 8:04 AM, Jonah-Horowitz notifications@github.com
wrote:
Matt Morgan
Director of Technology
Dulcet Software LLC
(734) 926-8142
Jonah-Horowitz commentedon Apr 3, 2016
Essentially, here's how I would have written the test suite:
or possible included some examples with
Strings.I'm not terribly good at creating exercises - could you elaborate?
matthewmorgan commentedon Apr 3, 2016
Interesting. Thanks for doing this.
In your example solution, there seems to be issue around the array creation here:
There's a discussion about this on SO here.
So, one possible solution is the approach I took, using reflection. Another appears to be type casting.
What do you propose? Is there a reason you would prefer on to the other? Let's discuss and see if we can get some other folks to weigh in too.
Jonah-Horowitz commentedon Apr 3, 2016
Hm, based on the link you provided, it's looking to me like my proposal might be impossible in Java. Particularly of interest was Bill Michell's answer (here). On the other hand, it looks like Radiodef's answer (here) and Lii's comment on it might offer something of a compromise using Java 8's
Functioninterfaces, although I suspect that the test suite would then have to employ Java's new lambda function capabilities (which may make it more confusing for people).So it looks to me now like there's three approaches that might work, each with its own drawback:
Yours which uses reflection (resource heavy, but that's probably irrelevant in an exercise):
One that uses just casting (not type safe):
And one that uses functional interfaces (relies on the caller to supply an array generator):
Thoughts?
matthewmorgan commentedon Apr 3, 2016
I agree that using
Functionmight be a bit too much for this exercise. Thelinked-listexercise doesn't offer any guidance really-- it uses generics, but doesn't return an array, so this issue doesn't arise.I looked at the JDK implementation, and here it is:
It looks to me like they favored using the reflection approach there, so it seems like that might be the way to go.
I did want to point out that your example solution contains something that I missed in mine, which is the parameterization of the
SimpleLinkedListclass itself, so regardless of what we do about thetoArraymethod I think my parameter oversight should be corrected.I still don't know if that gets us any closer to addressing your concerns though. Let me know.
matthewmorgan commentedon Apr 7, 2016
@Jonah-Horowitz unless you have further changes you want to suggest here, I'll
put in a PR to add the parameterclose this issue. Thanks!Tto the class declaration, andEdit: the parameter is in there already, closing the issue.