How does one cast a generic type in to a user-defined type?

How does one cast a generic type in to a user-defined type?



Context: We've been tasked with making a small interpreter thing. One of the goals that we was hinted at was for us to not only work with variables of type double, but also to eventually be able to work with any type of variable.


double



Way I figured to do this was for to read in the type as a String, and have value set as a generic variable. Then we would cast value to whatever Type said it should be.


type


String


value


value



I could probably do a bunch of if statements of say...


if(type.equals("String")) /*Cast Value to String*/



But that's so inelegant as to seem to be wrong. Also, what if the user were to define a new variable type? I don't think hard coding the possible types is the answer.


types



Question: Is there a way to, more or less, directly cast a generic variable in to a type that is inputted by the user?






Unrelated: in Java Variable names start lower case...

– GhostCat
Sep 12 '18 at 3:59






I know. I did it this way for readability's sake.

– JustAQuestion
Sep 12 '18 at 4:25






Don't do that, or people will think it's a static field.

– everton
Sep 12 '18 at 5:04






Ah. OK. I'll fix this.

– JustAQuestion
Sep 12 '18 at 5:13




2 Answers
2



Looks like there are two parts to your problem.


Type


parse


Type


Type






Hmm. I'll look in to the SPIs when I wake up. Sounds promising enough. But basically: There's no easy way to do what I am asking?

– JustAQuestion
Sep 12 '18 at 8:01






Easy is subjective. That said, SPI is really easy to use.

– Dakshinamurthy Karra
Sep 12 '18 at 8:11



i don't know what exactly do you need but in java you can find object types like this:


if (value.getClass() == String.class)
....



and:


if (value instanceof Object)
....



if you dont like to use bunch of if statements:


switch (obj.getClass().getName())
case "java.lang.String":
break;
case "java.lang.Integer":
break;
default:




and for cast your generic object to a custom type that given from user you can use this code :


Object obj = "";
String className = "java.lang.String";
try
Class aClass = Class.forName(className);
Object result = aClass.cast(obj);
catch (ClassNotFoundException e)
e.printStackTrace();



whit this code you can convert your object to your class name that give in string.






I didn't downvote your comment. However, the first part directly contradicts what I said I didn't want to do (a bunch of if statements for each type). And second, I may be incorrect, but I think that String.getClass() would just return a string, no matter what was said in the string..

– JustAQuestion
Sep 12 '18 at 4:24


String.getClass()






if you dont like to use bunch of if statements you can use a switch case, i add it in my post

– JNDanial
Sep 12 '18 at 4:34






That's...that's still the same thing...

– JustAQuestion
Sep 12 '18 at 5:13






now you can use it in one line like your need if(obj.getClass().getName().equals("java.lang.String")) /*....*/

– JNDanial
Sep 12 '18 at 5:26






you update your question and i think find your answer so i update my answer and i hope it can help you

– JNDanial
Sep 12 '18 at 9:27



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)