Home Casting Function Return Type Based on Class Field
Reply: 0

Casting Function Return Type Based on Class Field

user5050 Published in September 21, 2018, 8:36 am

I've got a class that looks something like.

public class ParseValue {
  public String value;
  public final Class classType;

And I'd like to make a function that does a conversion and returns a casted value.

public T parseValue(ParseValue parseInfo) {
  if(parseInfo.classType == String.class) {
    return parseInfo.value;
  } else if (parseInfo.classType == Double.class) {
    return Double.valueOf(parseInfo.value);

Right now I can have this function return an Object and then cast it upon getting the result, but is there a way to make the function do the cast based on the input ParseValue's classType field?

share|improve this question
  • 1
    Yes. But you'd need to make ParseValue generic and the Class field as well. – Elliott Frisch Feb 14 at 0:58

1 Answer 1

active oldest votes
up vote 3 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.414042 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO