There is the _= syntax for basic properties. Which are roughly Java's equivalents for the getters and setters. But is there also something comparable to Java's indexed properties ?
I'd like to make people's life easier to allow something like this:
Setting:
titles.title(1) = "title of 1" // returns nothing
Getting:
titles.title(1) // returns "title of 1"
Is that possible with Scala ?
UPDATE: Example code
class Foo {
val title = new IndexedProperty[Int, String]
}
class IndexedProperty[A, B] {
var map = Map.empty[A, B]
def apply(key: A): Option[B] = map.get(key)
def update(key: A, value: Option[B]): Unit = {
value match {
case Some(v) => map += (key -> v)
case None if map.contains(key) => map -= key
case _ =>
}
}
}
val foo = new Foo
foo.title(1) = Some("Title of 1")
println(a.title(1)) // yields Some("Title of 1")
println(a.title(2)) // yields None
Something like this maybe:
object titles {
object title {
def apply(i: Int) = "apply " + i
def update(i:Int, s: String) = "update " + i + " = " + s
}
}
Works the same way for classes as for objects.