Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

build function that return different types based on param  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I like to write simple java function that will return me different values some times String or Int or Boolean
based on given arg
some thing like :
if(arg==1){
return (int)foo;
}elseif(arg==2){
return (String)foo;

}elseif(arg==3){
return (boolean)foo;

}
What kind of function type I need to define my function?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do that???
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im getting Object[] array as arg then I
then in side this function I have another function that will cast me the elements from that array as I tell it for that I need this general java function
can it be done in java ?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't be able to make those casts work. You can't cast an object reference to a primitive.

Could you describe the full problem and show the rest of the code?
 
Timothy Frey
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're looking for is not possible in Java (or any statically typed language I would think). The closest you can get would be to return something of type Object but of course you're already given a whole bunch of Objects that you're trying to cast in the first place.

If you feel that you must keep your current design, you can try the "instanceof" operator in a huge if-else if block to do the casting. However, this problem has BAD DESIGN written all over it. Any reason why it's an array of Objects instead of something more meaningful? Are you given any clues as to what order the objects come in?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Frey:
What you're looking for is not possible in Java (or any statically typed language I would think).


And in a dynamically typed language, the casts wouldn't be necessary at all...

Meir, what would the clients of the method do with the returned value?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok im conversion some kind of in-house server script lang to jsp
in this lang I have function that called format that takes 2 args ;
Format.format("N",arg1) what it does is , first parameter is the type returned in this case "Number" , it can be also
Format.format("S",arg1) returned in this case "String" , and the second arg is the variable .

what I did ( its not perfect at all)
I made class called Format

public class Format {
public String format(String szFormat,int val){
String s = Integer.toString(val);
return s;
}
public String format(String szFormat,String val){
//int i = Integer.parseInt(val);
String i = val;
return i;
}
}

but as you can see I have problem when I have variable from type int that I what it to format it to int not string
or variable from type string and return it as int .
I hope I made my self clear im still looking for some way to do it �
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of overloading the method names, give the two methods different names. Then they can return different types.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I was thinking about it but I have something like 1000 pages to convert and I try to do minimum search and replace
I just need to know if there is some kind of trick to do it
Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So those methods will be called from JSPs? What will typical JSP code look like?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is working fine from jsp with the class
<%
Format ft = new Format();

%>
<option value="<%= ft.format("S",szName) +"|"+ ft.format("S",szUser) %> >
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the JSP and scriptlet business is going to convert the result of those methods to Strings anyway. So having the methods all return String shouldn't be a problem.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah but what if i have this case :
String szNum = "242";
int iVar = ft.format("N",szNum );

here i need it to return int based on "N"
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible in a statically typed language like Java. You will need to use different methods based on the return type.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the fast reply
maybe there is some trick or work around to this matter
basically what im looking for is some kind of factory pattern no?
can you please move this thread to the more advance java forum ?
thanks allot
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no workaround.

The point is that the compiler statically checks the types at compile time. More specifically, in an assignment it checks that the compile time type of the right side value is assignment compatible to the left side.

In your last example, the compile time type of the left hand side was int. So the method you call on the right side of the assignment operator has to have a return type that is assignment compatible to int. There is no way to declare that method so that in other circumstances, given other arguments, it could return a String.

Well, in fact there is a way, if you use Java 5 and use Class as the first parameter instead of String - then you could use generics:

public <T> T format(Class<T> format,int val)

For that method, the compiler knows that

format(String.class, 42);

returns a String, but

format(Integer.class, 42);

returns an Integer. Together with auto-unboxing, the line

int i = format(Integer.class, 42);

becomes valid.

But is that really better than having a formatAsInteger method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!