stephansun对《Scala in Depth》的笔记(5)

stephansun
stephansun (认真你就输了...)

读过 Scala in Depth

Scala in Depth
  • 书名: Scala in Depth
  • 作者: Joshua D. Suereth
  • 页数: 304
  • 出版社: Manning Publications
  • 出版年: 2012-5-24
  • 第69页
    When compiling traits, Scala creates an interface/implementation pair of classes. The interface is for JVM interoperability and the implementation is a set of static methods that can be used by classes implementing the trait. When compiling the Test object, a main method is created that forwards to the Application implementation class. Although this method is empty, the logic inside the Test object is placed in the Test object's constructor. Next, Scala creates "static forwarders" for the object. One of these static forwarder methods will be the main method, in the signature the JVM expects. The static forwarder will call the method on the singleton instance of the Test object. This instance is constructed in a static initialization block. And finally, we get to the issue. Code inside a static initialization block isn't eligible for HotSpot optimization. In fact, in older versions of the JVM, methods called from a static initialization block wouldn't be optimized either. In the most recent benchmarks, this has been corrected, such that only the static block itself isn't optimized.
    引自第69页
    2014-07-08 16:33:24 回应
  • 第107页
    override def toString = "Matrix" + repr.foldLeft(") { (msg, row) => msg + row.mkString("\n|" , " | " , "|") } 中的repr.foldLeft(")应为repr.foldLeft("")
    引自第107页
    2014-07-10 13:10:54 回应
  • 第76页
    When creating a hierarchy of mixable behaviors via trait, you need to ensure the following: - You have a mixin point that traits can assume as a parent - Your mixable traits delegates to their parent in meaningful ways - You provide default implementations for chain-of-command style methods at your mixin point.
    引自第76页
    2014-08-23 10:16:48 回应
  • 第40页
    In general, it's best to avoid polymorphism with types requiring deep equality. Scala non longer supports subclassing case classes for this very reason.
    引自第40页

    在《DSLs in Action》这本书里有一章Scala的例子,就是用了case class的subclassing,不过那个应该是老版本的Scala。

    2015-01-25 20:11:19 回应
  • 第3页

    针对 "A cat catches a bird and eats it" 这个story,在Scala-IDE中的worksheet代码 ``` package ch01 object section11_3 { trait Cat trait Bird trait Catch trait FullTummy def `catch`(hunter: Cat, prey: Bird): Cat with Catch = new Cat with Catch //> catch: (hunter: ch01.section11_3.Cat, prey: ch01.section11_3.Bird)ch01.secti //| on11_3.Cat with ch01.section11_3.Catch def eat(consumer: Cat with Catch): Cat with FullTummy = new Cat with FullTummy //> eat: (consumer: ch01.section11_3.Cat with ch01.section11_3.Catch)ch01.sectio //| n11_3.Cat with ch01.section11_3.FullTummy val story = (`catch` _).tupled andThen (eat _) //> story : ((ch01.section11_3.Cat, ch01.section11_3.Bird)) => ch01.section11_3 //| .Cat with ch01.section11_3.FullTummy = <function1> story(new Cat {}, new Bird {}) //> res0: ch01.section11_3.Cat with ch01.section11_3.FullTummy = ch01.section11_ //| 3$$anonfun$main$1$$anon$2@2cb4c3ab } ``` 注意到 (`catch` _).tupled andThen (eat _),书中是没有tupled的,但不加就编译不过。 参考链接:http://stackoverflow.com/questions/21680117/why-doesnt-function2-have-an-andthen-method

    2015-01-25 21:34:46 回应