标题可能不太清楚。这是我的问题。
让我们说我有一个特性,用一系列配置参数定义一个应用程序。这些参数包含在Map
中,其中一些参数具有默认值。
trait ConfApp {
val dbName: String
lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar")
}
所以我可以按如下方式创建自定义应用程序:
class MyApp extends ConfApp {
override val dbName = "my_app_db"
// print app configuration parameters
println(conf)
def add() = {...}
...
}
val M1 = new Myapp // Map(db -> my_app_db, foo -> bar)
我想创建其他特性来设置其他配置参数。换句话说,我希望能够做到这样的事情:
class MyApp2 extends ConfApp with LogEnabled {
override val dbName = "my_app2_db"
// print app configuration parameters
println(conf)
def add() = {...}
...
}
val M2 = new Myapp2 // Map(db -> my_app_db, foo -> bar, log -> true)
到目前为止,我已成功完成以下任务:
trait LogEnabled {
val conf: scala.collection.mutable.Map[String, Any]
conf("log") = true
}
trait LogDisabled {
val conf: scala.collection.mutable.Map[String, Any]
conf("log") = false
}
trait ConfApp {
val dbName: String
lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar")
}
class MyApp extends ConfApp {
val dbName = "my_app_db"
println(conf)
}
class MyApp2 extends ConfApp with LogDisabled {
val dbName = "my_app_db"
println(conf)
}
val M = new MyApp // Map(db -> my_app_db, foo -> bar)
val M2 = new MyApp2 // Map(log -> false, foo -> bar, db -> null)
但正如您在M2
中看到的那样,db
参数为null
。我无法理解我做错了什么。
真诚地,我不喜欢使用可变地图的所有方法,但我还没有设法做得更好。
答案 0 :(得分:3)
你仍然可以这样使用不可变Map
:
scala> trait ConfApp {
| val dbName: String
| def conf: Map[String, Any] = Map("db" -> dbName, "foo" -> "bar")
| }
defined trait ConfApp
scala> trait LogEnabled extends ConfApp {
| override def conf = super.conf.updated("log", true)
| }
defined trait LogEnabled
scala> trait LogDisabled extends ConfApp {
| override def conf = super.conf.updated("log", false)
| }
defined trait LogDisabled
scala> class MyApp extends ConfApp {
| val dbName = "my_app_db"
| println(conf)
| }
defined class MyApp
scala> class MyApp2 extends ConfApp with LogDisabled {
| val dbName = "my_app_db2"
| println(conf)
| }
defined class MyApp2
scala> new MyApp
Map(db -> my_app_db, foo -> bar)
res0: MyApp = MyApp@ccc268e
scala> new MyApp2
Map(db -> my_app_db2, foo -> bar, log -> false)
res1: MyApp2 = MyApp2@59d91aca
scala> new ConfApp with LogDisabled with LogEnabled {
| val dbName = "test1"
| println(conf)
| }
Map(db -> test1, foo -> bar, log -> true)
res2: ConfApp with LogDisabled with LogEnabled = $anon$1@16dfdeda
scala> new ConfApp with LogEnabled with LogDisabled {
| val dbName = "test2"
| println(conf)
| }
Map(db -> test2, foo -> bar, log -> false)
res3: ConfApp with LogEnabled with LogDisabled = $anon$1@420c2f4a
如果您需要val conf
代替def conf
,则可以执行此操作:
scala> class MyApp extends ConfApp {
| val dbName = "my_app_db"
| override val conf = super.conf
| println(conf)
| }
defined class MyApp
scala> new MyApp
Map(db -> my_app_db, foo -> bar)
res4: MyApp = MyApp@17ebbd2a
答案 1 :(得分:1)
如果我是你,我不会使用vals,因为初始化顺序存在很多问题。对于其中一个,您还可以在特征中使用自引用来指定它们必须与ConfApp
混合在一起。那说我做这样的事情:
trait LogEnabled { self: ConfApp =>
self.conf("log") = true
}
trait LogDisabled { self: ConfApp =>
self.conf("log") = false
}
trait ConfApp {
def dbName: String
lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar")
}
class MyApp extends ConfApp {
override def dbName = "my_app_db"
println(conf)
}
class MyApp2 extends ConfApp with LogDisabled {
override def dbName = "my_app_db"
println(conf)
}
似乎工作正常。