-2

I have a generic method that returns a boolean value in specific scenarios.

the method signature of my method is e.g.

public <T> T get() { return Boolean.true;}

Till here, it works fine with no compiler error. although, when I try to use the same in an if clause, like

if(get())

then it returns error saying

Type mismatch: cannot convert from Object to boolean

Also I doubt this is happening only with eclipse. this is an existing code, which I am trying to compile in my workspace.

Please help.

Update:

This article explains Target Type Inference well - https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html

and as explained below by @Codebender, TargetType inference works well for chained/nested methods, Although it looks like it will not work for language constructs like if, for, while etc. I tried to write a method below -

 public void test(boolean val)

and tried calling it like

test(get())

and it works fine.

Solution:

Finally in this code, I ended up adding explicit typecast like

if((Boolean)get())

and it worked for me.

Anand Vaidya
  • 1,374
  • 11
  • 26
  • `get()` is used as a rawtype here, hence it does return an `Object`. A single `Object` doesn´t represent a valid condition. – SomeJavaGuy Apr 13 '16 at 06:08
  • 1
    Can you share the full context please? Also, why does this method need to be generic? You know you're returning a boolean and your `if` statement treats it as a boolean - why not just define it as such? – Mureinik Apr 13 '16 at 06:09
  • How does this magical `get()` method work that it can return the value of any desired type without even knowing what that type is? The only thing such method can return without (rightly) upsetting the compiler is `null`. – Misha Apr 13 '16 at 06:12
  • You can actually use a generic method in an `if` statement these days: http://ideone.com/wdI4KM. But does it make sense? (Probably not...) Why is it generic if you're sure that it's returning a `Boolean`? – Radiodef Apr 13 '16 at 06:12
  • As I said, Its an existing code, I am trying to compile in my eclipse. Boolean.TRUE is not an actual statement, there is a bunch of method calls happening around, but eventually in this case, it returns Boolean. I am using java8, and eclipse Mars.2 – Anand Vaidya Apr 13 '16 at 06:33

4 Answers4

2

To do this:

 if(get())

You need had a boolean like a return type of get method like this:

  public <T> boolean get() {...
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1

The code will work fine in Java 8 (because of generalized type inference)... But not in Java 7 and lower.

See this answer of mine to know what generalized type inference is: https://stackoverflow.com/a/32294457/2775450 and also this: https://stackoverflow.com/a/28474319/2775450

Apart from that, you code also seems to have other errors, like it's Boolean.TRUE instead of Boolean.true and you should have made an explicit cast to T in the return statement.

Community
  • 1
  • 1
Codebender
  • 14,221
  • 7
  • 48
  • 85
  • yeah Boolean.TRUE might be a type. actually its not Boolean.TRUE, some other statement. I put Boolean.TRUE to simplify the statement. – Anand Vaidya Apr 13 '16 at 06:17
  • Actuaally I am compiling against java 8, and thats why I was surprised with this error. is there something to do with eclipse? apart from source level in compiler settings? – Anand Vaidya Apr 13 '16 at 06:23
  • @AnandVaidya, Not sure... It is supposed to work on Java 8... You may want to recheck if you are actually using Java-8... – Codebender Apr 13 '16 at 06:30
  • apparently what it looks like is - even in java8, the improved type interface still does not apply on language constructs like if, while, for etc. So I think I will have to change the code. Although its an existing one, and I assumed that its correct, looks like its wrong. – Anand Vaidya Apr 13 '16 at 07:20
0

Why don't you define your method as

public <T> boolean get() { return Boolean.TRUE;}

In this case you won't get a compile error and you define the return argument as it is. In your original definition you expect the method to return something of type T and not boolean.

Update: Your code does not make use of generics. And there is no method where you could use a generic return value in an if statement you plan to use because Java does not have "falsy" values like script languages do (Python for example). An alternative to have a generic approach here is to add some generic value as a parameter and do something with it in the method body:

public <T> boolean get(T some value) { return Boolean.TRUE;}

But if you want to use the result in an if then consider adding something like this:

if(get() != null) {...}

It will work with a generic approach too. If you always return a boolean value from get() you do not need to use generics.

GHajba
  • 3,665
  • 5
  • 25
  • 35
0
I think this is happening because if you see in the method signature 
public <T> T get(), you are making it Generics but in the return statement, boolean value is returned. So to make it work just try to change the return type in the method signature like below:- 
public static <T> Boolean get() { 
        return Boolean.TRUE;
}
then use it in the if condition :- if(get()){   }
Mukesh
  • 540
  • 1
  • 9
  • 13