Results 1 to 5 of 5
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Handling generic arrays, the target type of this expression must be a functional inte

    Hello,

    Java Code:
    import java.util.function.Function;
     
    public class ParaFunction<I, O> implements Function<I[], O[]> {
        public Function<I, O> functions[];
     
        public ParaFunction(Function<I, O> functions[]) {
            this.functions = functions;
        }
     
        @Override
        public O[] apply(I inputs[]) {
            @SuppressWarnings("unchecked")
    		O outputs[] = (O[]) new Object[functions.length];
            for (int i = 0; i < functions.length; i++) {
                outputs[i] = functions[i].apply(inputs[i]);
            } return outputs;
        }
     
        public static void main(String[] args) {
        	Function<String, String> array[] = (Function<String, String>[]) new Object[] {
        		(String x) -> x + "a",
        		(String x) -> x + "b"
        	};
     
        	ParaFunction<String, String> func = new ParaFunction<String, String>(array);
        	System.out.println(func.apply(new String[] {"hello", "world"}));
        }
    }
    I get error:

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The target type of this expression must be a functional interface
    	The target type of this expression must be a functional interface
     
    	at com.pure.library.function.ParaFunction.main(ParaFunction.java:23)
    How do I solve this, such that what I'm trying to do, as inferred from the code presented, will work?
    Any and all comments, advise and help is appreciated!

    Kind Regards,
    augustas656
    Regards
    augustas656

  2. #2
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Handling generic arrays, the target type of this expression must be a functional

    I've tried to replace the main function with:

    Java Code:
    public static void main(String[] args) {
        	@SuppressWarnings("unchecked")
    		Function<String, String> array[] = (Function<String, String>[]) new Object[] {
    			new Function<String, String>() {
        			@Override
        			public String apply(String text) {
        				return text + "a";
        			}
        		}
        	};
        	
        	ParaFunction<String, String> func = new ParaFunction<String, String>(array);
        	System.out.println(func.apply(new String[] {"hello", "world"}));
        }
    But then I get error:

    Java Code:
    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.util.function.Function;
    	at com.pure.library.function.ParaFunction.main(ParaFunction.java:23)
    Kind Regards,
    augustas656
    Regards
    augustas656

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Handling generic arrays, the target type of this expression must be a functional

    Generics and arrays don't play well together. The reason is that generic type information is erased at
    runtime but array types are reified so they are available at runtime.

    So List<Integer>[] and List<String>[] both look like List[] at runtime. So your casts will be unchecked.

    I recommend you use a List implementation instead of an array. Here is an example of what I think you wanted to do. First is
    your way and then the preferred way.

    In any event your line 26 is incorrect in the first code segment because the function does not take an array of strings it takes a
    single string.

    Java Code:
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Function;
    
    public class GenericArray {
    
       public static void main(String[] args) {
          yourWay();
          preferredWay();
       }
    
       public static void yourWay() {
          @SuppressWarnings("unchecked")
          Function<String, String>[] fncs =
                (Function<String, String>[]) new Function[] {
                      (Object x) -> x + "a", (Object x) -> x + "b"
          };
          for (Function<String,String> f : fncs) {
             System.out.println(f.apply("a"));
          }
       }
    
       public static void preferredWay() {
          List<Function<String, String>> fncList =
                Arrays.asList(
                      (String x) -> x + "a",
                      (String x) -> x + "b"
                );
    
          for (Function<String,String> f : fncList) {
             System.out.println(f.apply("a"));
          }
       }
    
    }
    Regards,
    Jim
    Last edited by jim829; 04-01-2016 at 06:44 AM. Reason: make for-loops consistent
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Handling generic arrays, the target type of this expression must be a functional

    Quote Originally Posted by jim829 View Post
    Generics and arrays don't play well together. The reason is that generic type information is erased at
    runtime but array types are reified so they are available at runtime.

    So List<Integer>[] and List<String>[] both look like List[] at runtime. So your casts will be unchecked.

    I recommend you use a List implementation instead of an array. Here is an example of what I think you wanted to do. First is
    your way and then the preferred way.

    In any event your line 26 is incorrect in the first code segment because the function does not take an array of strings it takes a
    single string.

    Java Code:
    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Function;
    
    public class GenericArray {
    
       public static void main(String[] args) {
          yourWay();
          preferredWay();
       }
    
       public static void yourWay() {
          @SuppressWarnings("unchecked")
          Function<String, String>[] fncs =
                (Function<String, String>[]) new Function[] {
                      (Object x) -> x + "a", (Object x) -> x + "b"
          };
          for (Function<String,String> f : fncs) {
             System.out.println(f.apply("a"));
          }
       }
    
       public static void preferredWay() {
          List<Function<String, String>> fncList =
                Arrays.asList(
                      (String x) -> x + "a",
                      (String x) -> x + "b"
                );
    
          for (Function<String,String> f : fncList) {
             System.out.println(f.apply("a"));
          }
       }
    
    }
    Regards,
    Jim
    No my line 26 is correct in the first code segment because func is an instance of the class ParaFunction which implements Function<I[], O[]> hence an array of Inputs, which are type String in my main function meaning func.apply accepts an array of Strings or? As for List implementation, I'll see what I can do.

    Kind Regards,
    augustas656

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Handling generic arrays, the target type of this expression must be a functional

    Quote Originally Posted by augustas656 View Post
    No my line 26 is correct in the first code segment because func is an instance of the class ParaFunction which implements Function<I[], O[]> hence an array of Inputs, which are type String in my main function meaning func.apply accepts an array of Strings
    Yeah! I missed that. Simply change your lambda expressions to use (Object x) instead of (String x). Then use
    Arrays.toString() in your println statement to print your newly created array. Presently you are simply printing
    an array object. You will still get unchecked warnings unless you suppress them so I recommend you use lists.

    Regards,
    Jim
    Last edited by jim829; 04-01-2016 at 06:41 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2013, 01:44 PM
  2. Replies: 2
    Last Post: 12-21-2012, 05:32 AM
  3. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  4. Type wildcards in Generic
    By gvm in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 05:39 PM
  5. Generic collection type
    By dreuzel in forum Advanced Java
    Replies: 7
    Last Post: 12-23-2009, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •