gpt4 book ai didi

scala - 保理应用特征

转载 作者:行者123 更新时间:2023-12-01 02:41:38 27 4
gpt4 key购买 nike

我有一个使用 App 的应用程序trait,而且一切正常。但是object继承自 App变得有点笨拙,所以我想将一些功能拆分为特征。特别是,我想拆分命令行参数处理。

不幸的是,这似乎不适用于 DelayedInit .所以这很好用:

object Main extends App {
println("arguments are: "+ args.mkString.mkString(", "))
}

但这会抛出 NullPointerException :
trait CommandLineArguments { this: App =>
println("arguments are: "+ args.mkString.mkString(", "))
}

object Main extends App with CommandLineArguments

有什么办法可以拿到 DelayedInit “包括”混合特征?

最佳答案

看起来很奇怪,如果像这样写一些,效果很好:

trait CommandLineArguments { self: App => 
delayedInit {
println("arguments are: "+ args.mkString.mkString(", "))
}
}

但是来自 scaladocs (http://www.scala-lang.org/api/current/index.html#scala.DelayedInit):

Classes and traits inheriting the DelayedInit marker trait will have their initialization code rewritten as follows.[Code] becomes delayedInit([Code]) Initialization code comprises all statements and all value definitions that are executed during initialization.

关于scala - 保理应用特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com