How could I (during runtime) decide which type to return from my function?
Is this possible at all?
I think it is but one can never be sure.
How could I (during runtime) decide which type to return from my function? Is
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use polymorphism
then simply return type of
MyTypeand calldoSomething()directly on that object; ie you don’t need to know whether the returned type isAorB. All you care about is that it implementsdoSomething. That is the beauty of polymorphism, i.e. no more uglyis,getType,instanceOf(Java), etc.