3

I am using a java object that returns a java interface and am a newbie with scala I am trying to do the following:

val model = dao.getAccount(id)

model is an interface but I should be able to cast it as an object

but I know that in java I can cast this object like this:

model = (AccountModel) accountDAO.getAccount(id)

How do I do this in a scala app?

I tried this: retVal.asInstanceOf(AccountModel) but it doesn't seem to like that

1 Answer 1

11

You're almost there. It's retVal.asInstanceOf[AccountModel] (note the square brackets).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, thats it - doing java for over 10 years, trying to pick this up, not sure its worth it ;)
If you've been stuck in the Java monoculture for 10 years, you desperately need to do something else - for your sanity if nothing else ;-)
"Orangutans are skeptical / Of changes in their cages / And the zookeeper is very fond of rum." -- Paul Simon
@user197630 Scala is very regular. AccountModel is a type, and types are passed between square brackets. The same syntax that works for asInstanceOf works for every other method where you might want to pass the type parameter, and is reflected by the declaration of type parameters.
@user197630 If you still like Java after 10 years, it must be the Stockholm Syndrome...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.