gpt4 book ai didi

scala - 为什么我在 Scala 中得到 Some 而不是 String

转载 作者:行者123 更新时间:2023-12-03 15:08:22 25 4
gpt4 key购买 nike

为什么我得到的是 [Some] 对象而不是 [String] 对象?
Some 对象在方法调用中不能用作字符串参数。
config def返回 String所以我希望类型是 String .但是当我输入“Hello”时,Scala 是正确的。

代码

    def config(s: String) = Play.current.configuration.getString(s).toString()
Logger.info(config("recaptcha.publicKey"))
Logger.info("Hello")

输出
[info] application - Some(6LeDMdASAAAAAC4CFIDY-5M7NEZ_WnO0NO9CSdtj)
[info] application - Hello

最佳答案

您不必要地调用 toString()Option[String] (Play.current.configuration.getString() 返回),试试这个:

def config(s: String) = Play.current.configuration.getString(s).get

或者最好是:
Play.current.configuration.getString(s).getOrElse("some default")

关于scala - 为什么我在 Scala 中得到 Some 而不是 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149027/

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