For example suppose I have
abstract class OrderRouter extends Market {
}
I would normally instantiate as follows
new OrderRouter with NYSE
How would I translate the above line into java? NYSE is a trait which extends Market.
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.
Have a look at the question How are Scala traits compiled into Java bytecode?, and the accepted answer.
The summary is that, assuming NYSE has a
getOrderBook()method, the Java version would look like:The Scala compiler generates bytecode for synthetic classes, which mix in all of the trait implementations via composition/wrapping. Since
javacdoesn’t have this feature, you need to wire in the delegation of trait methods to the trait’s singleton object yourself.