Timeline for implement cons function in Java - type safety question
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 11, 2013 at 19:01 | comment | added | lwm | brilliant, I had no idea you could use type params that way. | |
Mar 11, 2013 at 19:01 | vote | accept | lwm | ||
Mar 11, 2013 at 18:55 | comment | added | sepp2k |
@LukeMurphy I just realized it has to be YourClass.<T>cons even if YourClass is the current class. Does that work? If no, please post your whole code, so I can try to compile it myself.
|
|
Mar 11, 2013 at 18:43 | comment | added | lwm | Unfortunately no... | |
Mar 11, 2013 at 18:38 | comment | added | sepp2k |
@LukeMurphy <T> cons() does not work?
|
|
Mar 11, 2013 at 18:34 | comment | added | lwm | '(Function2<List<T>,T,List<T>>) cons()' is the only syntactically correct type params I can give...and it still gives me an unchecked error. nice one anyway, java wins ;) | |
Mar 11, 2013 at 18:21 | comment | added | sepp2k |
@LukeMurphy Ah, you might have to give type parameters to cons as well.
|
|
Mar 11, 2013 at 18:14 | comment | added | lwm | I already tried giving the emptylist a type, but no luck there. Here is the error without the cast: (EmptyList, EmptyList<T> seems to make no difference) Cannot resolve method 'foldl(Function2<List<java.lang.Object>,java.lang.Object, List<java.lang.Object>>, EmptyList, List<T>)' I am starting to assume this has something to do with the fact I am using closures for my cons function? | |
Mar 11, 2013 at 17:58 | comment | added | sepp2k |
@Luke Oh, you're right I read it wrong. Sorry. The real problem is that new EmptyList is missing the type parameter - I think. If that's not it, post the error message you get without the cast. And what I meant by it being hard coded is that you should have one version that can handle any possible result type (using a second type parameter).
|
|
Mar 11, 2013 at 17:52 | comment | added | lwm | I have two versions of foldl, one with the accumulator as a T, and one with the accumulator as a List<T>, so it isn't so much 'hard coded'. The function passed to the foldl takes a T, List<T> , cons takes a T, List<T> and reverse returns a List<T> ? The cast does work... | |
Mar 11, 2013 at 16:53 | comment | added | sepp2k | @LukeMurphy And it isn't really accurate to say "I have to use a cast" as that would imply the cast works, which I'm pretty sure it won't. | |
Mar 11, 2013 at 16:51 | comment | added | sepp2k |
@LukeMurphy Hard-coding the result type of foldl to List<T> doesn't seem like a good idea. Anyway your problem is that the function given to fold takes a list and a T, while cons takes a T and a list. So you either need to reverse the order in which the function given to fold takes its argument, the order in which cons takes its arguments or define a flip function and pass flip cons as the function argument to foldl .
|
|
Mar 11, 2013 at 16:43 | comment | added | lwm | sorry to demote your answer, your recommendations were correct, but my original problem still persists... | |
Mar 11, 2013 at 15:49 | history | edited | sepp2k | CC BY-SA 3.0 |
deleted 3 characters in body
|
Mar 11, 2013 at 15:20 | vote | accept | lwm | ||
Mar 11, 2013 at 16:43 | |||||
Mar 11, 2013 at 9:28 | history | edited | sepp2k | CC BY-SA 3.0 |
added 110 characters in body
|
Mar 10, 2013 at 18:34 | history | answered | sepp2k | CC BY-SA 3.0 |