Scala slow builds: development approaches to avoid

后端 未结 2 886
忘了有多久
忘了有多久 2021-02-07 12:50

First of all, incremental builds via SBT are pretty awesome, generally in the < 1sec range. However, sometimes you have to do a full clean/compile, or, in the case of increme

相关标签:
2条回答
  • 2021-02-07 13:11

    Have a look at how incremental recompilation works in SBT.

    It's roughly this:

    1. Find all classes whose publicly visible API have changed
    2. Invalidate all of its dependents, its dependents' dependents, and so on.

    For the purposes of SBT, a "dependent" is both a user of the class and a class defined in the same file.

    Owen's example for foo.scala could even be this, and you'd see the issue:

    object foo {
      def z: Int = 8
    }
    
    object foo2 {
      class A { ... }
    }
    

    Good practices:

    • Separate files for separate classes
    • Fine-grained interfaces
    • Use the same level of abstraction in companion objects as in their companion classes; if the companion object reaches up through layers of abstraction, pull it into a separate class and file.
    0 讨论(0)
  • 2021-02-07 13:13

    I've noticed that type members can force rebuilds in places you would not expect. For example:

    foo.scala:

    object foo {
        class A {
            type F = Float
        }
        def z: Int = 8
    }
    

    bar.scala:

    object bar {
        def run { println(foo.z) }
    }
    

    Changing the value of z does not force bar to be recompiled. Changing the type of F does, even though bar never refers to F or even to A. Why, I have no idea (Scala 2.9.1).

    0 讨论(0)
提交回复
热议问题