How can I implement a Class which can ask just like Array, providing indexed setter?
like:
val k = new MyKls(size)
k(0) = 2 //<<-- I want this kind of functionality.
Thanks.
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.
It sounds like what you are looking for is the
updatemethod. If you define this method, then Scala will use it in thek(0) = 2syntax. It is similar to theapplymethod which allows you do use thek(0)accessor syntax.Here’s a short example:
You can find some more detail here.