Re: Re: Combinators
Simon Schäfer <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
On 01/31/2015 05:59 PM, [email protected] wrote: > Also what is this line doing here? > > implicit def kestrel[A](a: A) = new { > > What does "new" do in this context? a new function? It represents an anonymous class, in short notation: scala> new {} res0: AnyRef = $anon$1@67424e82 scala> new Object {} res1: Object = $anon$1@51521cc1 > > On Saturday, January 31, 2015 at 10:50:05 AM UTC-6, > [email protected] wrote: > > I started reading Scala in Action as my first Scala book (I've > come to realize this book is way too advanced for beginners, i am > struggling mightily). > > I need help with the following. > > object Combinators { > implicit def kestrel[A](a: A) = new { > def tap(sideEffect: A => Unit): A = { > sideEffect(a) > a > } > } > } > > case class Person(firstName: String, lastName: String) > case class Mailer(mailAddress: String) { > def mail(body: String) = { > println("send mail here...") > } > } > > object Main { > import Combinators._ > def main(args: Array[String]): Unit = { > Person("Nilanjan", "Raychaudhuri").tap(p => { > println("First name " + p.firstName) > Mailer("some address").mail("new person joined " + p) > }).lastName > } > } > > My questions are as follows: > 1) How does Person automatically get access to "tap"? > 2) What does the implicit keyword do here? (am guessing this > might answer q #1) > 3) Any other book suggestions for a Java guy trying to learn > Scala. (I've got about 10 years of Java experience, if that helps). > > Thanks in advance. > > -- > You received this message because you are subscribed to the Google > Groups "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected] > <mailto:[email protected]>. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.