Re: Combinators
Vlad Patryshev <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAFhNTo_J1H0EZfTTesTSVE5qK9oC7rtKBMCEt4-RRQwDyrWPow@mail.gmail.com> |
As I see, you will need to figure out how implicit works. You can google it, or read some kind of introduction to Scala. In short, it is *implicit*, and is applied when compiler finds it necessary. Thanks, -Vlad On Sat, Jan 31, 2015 at 8:50 AM, <[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]. > 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.