kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Suggestion: Generic Functions.toStringFunction() #439

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a test where I need to construct an object that takes a 
Function<MySpecificDataClass, String>.  It would be nice to  be able to re-use 
toStringFunction for that purpose since my test doesn't depend on a specific 
function implementation.  However, Function<Object, String> cannot be cast to 
Funciton<MySpecificDataClass, String>.

Original issue reported on code.google.com by boppenh...@google.com on 1 Oct 2010 at 1:04

GoogleCodeExporter commented 9 years ago
 I didn't realize that being a project member meant my issues were marked as accepted by default.

Original comment by boppenh...@google.com on 1 Oct 2010 at 1:05

GoogleCodeExporter commented 9 years ago
Your test should be taking Function<? super T, String>, right? Or are you 
saying that you need to conform to some code that isn't doing proper PECS 
generics?

You could always cast it with a @SuppressWarnings...

@SuppressWarnings("unchecked")
public static <T> Function<T, String> convert (Function<Object, String> func)
{
   return (Function<T, String>)func;
}

Then you can pass 
YourClass.<MySpecificDataClass>convert(Functions.toStringFunction());

Original comment by ray.j.gr...@gmail.com on 1 Oct 2010 at 2:21

GoogleCodeExporter commented 9 years ago
Yes, but that turns out to be just as many lines/effort as providing my own 
toString function implementation inline.

Original comment by boppenh...@google.com on 1 Oct 2010 at 3:30

GoogleCodeExporter commented 9 years ago
You're probably right, I'll see about modifying the code that I'm working with 
to use proper generics.

Original comment by boppenh...@google.com on 1 Oct 2010 at 3:42

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09