diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2017-02-15 21:58:46 -0500 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2017-02-19 12:12:38 +0800 |
commit | 77a4529342ab58c0899e5add35edecb7d1305c8d (patch) | |
tree | de0e3b266917d4323543e1c429cbb1125edf6360 /README.md | |
parent | cf1cf8678e7ad5366c9ac40451cfa9353bd8b7f3 (diff) | |
download | cbt-77a4529342ab58c0899e5add35edecb7d1305c8d.tar.gz cbt-77a4529342ab58c0899e5add35edecb7d1305c8d.tar.bz2 cbt-77a4529342ab58c0899e5add35edecb7d1305c8d.zip |
Document common inheritance pitfalls
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -495,3 +495,36 @@ only show first 20 lines of type errors to catch the root ones ``` cbt c 2>&1 | head -n 20 ``` + +Inheritance Pittfalls +--------------------- +``` +trait Shared extends BaseBuild{ + // this lowers the type from Seq[Dependency] to Seq[DirectoryDependency] + override def dependencies = Seq( DirectoryDependency(...) ) +} +class Build(...) extends Shared{ + // this now fails because GitDependency is not a DirectoryDependency + override def dependencies = Seq( GitDependency(...) ) +} +// Solution: raise the type explicitly +trait Shared extends BaseBuild{ + // this lowers the type from Seq[Dependency] to Seq[DirectoryDependency] + override def dependencies: Seq[Dependency] = Seq( DirectoryDependency(...) ) +} +``` + +``` +trait Shared extends BaseBuild{ + override def dependencies: Seq[Dependency] = Seq() // removes all dependencies, does not inclide super.dependencies +} +trait SomePlugin extends BaseBuild{ + // adds a dependency + override def dependencies: Seq[Dependency] = super.dependencies ++ Seq( baz ) +} +class Build(...) extends Shared with SomePlugin{ + // dependencies does now contain baz here, which can be surprising +} +// Solution can be being careful about the order and using traits instead of classes for mixins +class Build(...) extends SomePlugin with Shared +``` |