Created
December 17, 2015 17:24
-
-
Save toff63/b6c3366d8cb343c1355c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * In Scala it is a good practice to keep configuration in traits. | |
| * The main reason is that you can easily override them programmatically | |
| * using the fact that the last trait imported to a class will win in case of override | |
| */ | |
| // Default configuration | |
| trait Configuration { | |
| lazy val defaultName:String = "default" | |
| } | |
| // Class using configuration | |
| class Person extends Configuration { | |
| def getDefaultName:String = defaultName | |
| } | |
| // Configuration I want to use in a specific case, test for example | |
| trait ConfigurationOverride extends Configuration{ | |
| override lazy val defaultName:String = "override" | |
| } | |
| class PersonWithNewConfiguration extends Person with ConfigurationOverride | |
| object OverrideConfiguration { | |
| new Person().getDefaultName //> res0: String = default | |
| new PersonWithNewConfiguration().getDefaultName //> res1: String = override | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment