- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这比其他任何事情都更像是一个设计问题......
我真的很喜欢 Scala 的案例类并且经常使用它们。但是,我发现我经常将参数包含在 Options
中。 (或者更确切地说,Lift 的 Boxes
)并设置默认值以实现灵活性并考虑到用户可能并不总是指定所有参数。我想我采用了这种做法。
我的问题是,这是一种合理的方法吗?鉴于一切都可能是可选的,可能会有很多样板和检查,以至于我怀疑我是否不只是使用我的案例类,如 Map[String, Any]
并想知道我是否会更好地使用 Map
.
让我给你一个真实的例子。在这里,我正在模拟汇款:
case class Amount(amount: Double, currency: Box[Currency] = Empty)
trait TransactionSide
case class From(amount: Box[Amount] = Empty, currency: Box[Currency] = Empty, country: Box[Country] = Empty) extends TransactionSide
case class To(amount: Box[Amount] = Empty, currency: Box[Currency] = Empty, country: Box[Country] = Empty) extends TransactionSide
case class Transaction(from: From, to: To)
Transaction
像这样:
val t = Transaction(From(amount=Full(Amount(100.0)), To(country=Full(US)))
val t2 = Transaction(From(Full(Amount(100.0, Full(EUR))), Full(EUR), Full(Netherlands)), To(Full(Amount(150.0, Full(USD))), Full(USD), Full(US)))
Full
在任何地方,你仍然可以做一些很好的模式匹配:
t2 match {
case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(country_from)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(country_to))) if country_from == country_to => Failure("You're trying to transfer to the same country!")
case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(US)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(North_Korea))) => Failure("Transfers from the US to North Korea are not allowed!")
case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(country_from)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(country_to))) => Full([something])
case _ => Empty
}
Map
会为我提供更好的服务吗? ?或者我应该以不同的方式使用案例类?也许使用整个案例类层次结构来表示具有不同信息量的交易?
最佳答案
如果某些东西真的是可选的,那么你真的别无选择。 null
是 不是 一个选项(没有双关语意)。
但是,我强烈建议不要使用 Lift 的 box 类型,除非您需要它来专门处理 Lift API。您只是引入了不必要的依赖项。
我还会认真考虑拥有一个 Amount
是否真的有意义。没有指定的货币。如果它是有效的,那么创建一个专用的“空对象”来表示一个未指定的货币会给你一个更清晰的 API:
class LocalCurrency extends Currency
sealed trait Amount
case class LocalisedAmount(value: Double, currency: Currency) extends Amount
case class RawAmount(value: Double) extends Amount
TransactionSide
子类,我觉得奇怪的是你可以指定
Currency
与
Amount
分开(它已经嵌入了货币的概念)。我赞成:
case class TxEnd(
amount: Option[Amount] = None,
country: Option[Country] = None)
case class Transaction(from: TxEnd, to: TxEnd)
关于scala - 使用 Scala 案例类作为事实上的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6547409/
我一直在使用 Indeed.com XML Feed API 为测试应用程序收集工作职位。 API 似乎因我们的服务器 IP 而被阻止并抛出以下错误。 curl: (56) Recv failure:
首先我想为我的英语感到抱歉。 我开始做一些单元测试(我以前从未这样做过,我是编程新手)。 我必须使用mockito.verify测试将产品添加到数据库(DynamoDB)的简单方法,但我有 "Want
我正在学习计算机体系结构,并遇到了一种说法:“完全可以在没有虚拟内存的情况下运行,只需物理内存(事实上,大多数嵌入式系统都以这种方式运行)”。 这是真的吗?如果是,那么我想知道如何? 谢谢。 最佳答案
我正在尝试使用函数过滤器设置规则。但它不起作用:该层上的线条只是黑色。更重要的是,我提供的用于过滤特征的函数从未被调用。 谁能指出我犯的错误是什么? 这是代码。 感谢您的时间和亲切的关心。 var m
我正在尝试使用 Hibernate 和 JPA 调试预先存在的代码。该错误消息是有道理的,甚至还有其他几个问题正在讨论。但是当我寻找一个保持打开的 session 时,我发现整个代码正文中没有提到“
我是一名优秀的程序员,十分优秀!