--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-debate+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
implicit class FluentExtensionMethod[A](private val self: A) {
def fluently(actions: (A => Unit)*) = actions.foreach(_(self))
}
person.fluently(
_.setName("Jack"),
_.setAge(55),
_.setCountry("US")
)
--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-debate...@googlegroups.com.
I got a feeling that this fluent approach is only good in OOP with mutable data.
Why? The signature is mutable/immutable agnostic. For mutable you return this
, for immutable you return new
.
trait Foo {
def name: String
def name(n: String): this.type
def limit: Int
def limit(l: Int): this.type
}
class MutableFoo
extends Foo {
private var _name: String = _
private var _limit: Int = _
def name: String = _name
def name(n: String) = {
_name = n
this
}
def limit: Int = _limit
def limit(l: Int) = {
_limit = l
this
}
}
case class ImmutableFoo(name: String, limit: Int)
extends Foo {
def name(n: String) = this.copy(name = n).asInstanceOf[this.type]
def limit(l: Int) = this.copy(limit = l).asInstanceOf[this.type]
}
There's a reason you need the asInstanceOf, you aren't really conforming to the singleton type
--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-debate...@googlegroups.com.
There's a reason you need the asInstanceOf, you aren't really conforming to the singleton type
To unsubscribe from this group and stop receiving emails from it, send an email to scala-debate+unsubscribe@googlegroups.com.
Kind regards,
Jasper
kr,
Jasper