Is it possible to chain values classes without them actually being instantiated? For example:
import scala.swing._
object SwingPlus {
implicit class RichComponent(val self: Component) extends AnyVal {
def clientProps: ClientProperties = new ClientProperties(self)
}
implicit class ClientProperties(val self: Component) extends AnyVal {
def += (kv: (String, Any)): Component = {
self.peer.putClientProperty(kv._1, kv._2)
self
}
}
}
Use case:
import SwingPlus._
val b = Button("Foo") { println("bar") }
b.clientProps += "JButton.buttonType" -> "textured"
val f = new Frame { contents = but; pack(); open() }
In the call b.clientProps +=
, does ClientProperties
get instantiated, or are these just pure static (optimised) method calls?
They are static calls. That's what value classes do: http://vptheron.github.io/blog/2013/11/02/a-word-of-implicit-conversions-and-value-classes/