- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题可能有点困惑,但意图是这样的:
我想将一个值限制为参数类型层次结构中的另一个值。鉴于它们都是类型,如果能强制执行与此类似的代码,那就太好了。
sealed trait Direction
case object Buy extends Direction
case object Sell extends Direction
case class Trade[D <: Direction](id: Long, itemId: Long, quantity: Int)
case class Hedge[D <: Direction, ??? -> O is D's OppositeDirection](id: Long, itemId: Long, openingTrade: Trade[D], closingTrade: Trade[O])
val trade1 = Trade[Buy](id = 1, itemdId = 10, quantity = 100)
val trade2 = Trade[Sell](id = 2, itemdId = 10, quantity = -100)
val validHedge = Hedge[Buy, Sell](id = 563, itemId = 10, openingTrade= trade1, closingTrade = trade2)
val invalidHedge = Hedge[Buy, Sell](id = 563, itemId = 10, openingTrade= trade1, closingTrade = trade1)
object NotEquality {
trait =!=[A, B]
implicit def neq[A, B] : A =!= B = null
implicit def neqAmbig1[A] : A =!= A = null
implicit def neqAmbig2[A] : A =!= A = null //Without this duplicated line, the compiler will ignore the restriction and silently fail. Why?
}
object Trades {
sealed trait Direction
case object Buy extends Direction
case object Sell extends Direction
import NotEquality._
case class Trade[D <: Direction](id: Long, itemId: Long, quantity: Int)
case class Hedge[D <: Direction, O <: Direction](id: Long, itemId: Long, openingTrade: Trade[D], closingTrade: Trade[O])(implicit ev: D =!= O)
}
object TradesNeq extends App {
import NotEquality._
import Trades._
val trade1 = Trade[Buy.type](1,10,100)
val trade2 = Trade[Sell.type](2,10,-100)
val validHedge = Hedge[Buy.type, Sell.type](id = 563, itemId = 10, openingTrade= trade1, closingTrade = trade2)
println(s"Valid Hedge: ${validHedge}")
// val invalidHedge = Hedge[Buy.type, Buy.type](563,10,trade1,trade1)
// println(s"InvalidHedge ${invalidHedge}")
}
object TradesDOImpl extends App {
sealed trait Direction
case object Buy extends Direction
case object Sell extends Direction
class TradeDirection[D <: Direction, Op <: Direction]
implicit val BuyToSell = new TradeDirection[Buy.type, Sell.type]
implicit val SellToBuy = new TradeDirection[Sell.type, Buy.type]
case class Trade[D <: Direction](id: Long, itemId: Long, quantity: Int)
case class Hedge[D <: Direction, O <: Direction](id: Long, itemId: Long, openingTrade: Trade[D], closingTrade: Trade[O])(implicit d: TradeDirection[D, O])
val trade1 = Trade[Buy.type](id = 1, itemId = 10, quantity = 100)
val trade2 = Trade[Sell.type](id = 2, itemId = 10, quantity = -100)
val validHedge = Hedge[Buy.type, Sell.type](id = 563, itemId = 10, openingTrade= trade1, closingTrade = trade2)
println(s"Valid Hedge: ${validHedge}")
val invalidHedge = Hedge[Buy.type, Buy.type](563,10,trade1,trade1)
println(s"InvalidHedge ${invalidHedge}")
}
object TradesOpposite extends App {
sealed trait Direction
abstract class OppositeDirection[D <: Direction](val opposite: D) extends Direction
abstract class Buy(opposite: Sell) extends OppositeDirection[Sell](opposite)
case object Buy extends Buy(Sell)
abstract class Sell(opposite: Buy) extends OppositeDirection[Buy](opposite)
case object Sell extends Sell(Buy)
case class Trade[D <: Direction](id: Long, itemId: Long, quantity: Int)
case class Hedge[D <: Direction, O <: OppositeDirection[D]](id: Long, itemId: Long, openingTrade: Trade[D], closingTrade: Trade[O])
val trade1 = Trade[Buy](id = 1, itemId = 10, quantity = 100)
val trade2 = Trade[Sell](id = 2, itemId = 10, quantity = -100)
val validHedge = Hedge[Buy, Sell](id = 563, itemId = 10, openingTrade= trade1, closingTrade = trade2)
println(s"Valid Hedge: ${validHedge}")
// val invalidHedge = Hedge[Buy, Buy](563,10,trade1,trade1)
// println(s"InvalidHedge ${invalidHedge}")
}
最佳答案
您可以将每个 Direction
对象的类型参数化为其相反 Direction
的类型:
trait OppositeDirection[D <: Direction] extends Direction
trait Buy extends OppositeDirection[Sell]
case object Buy extends Buy
trait Sell extends OppositeDirection[Buy]
case object Sell extends Sell
case class Hedge[D <: Direction, O <: OppositeDirection[D]](...)
abstract class OppositeDirection[D <: Direction](val opposite: D) extends Direction
abstract class Buy(opposite: Sell) extends OppositeDirection[Sell](opposite)
case object Buy extends Buy(Sell)
abstract class Sell(opposite: Buy) extends OppositeDirection[Buy](opposite)
case object Sell extends Sell(Buy)
关于Scala:基于同一层次结构的另一个类型参数值限制参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23804958/
SELECT ID, AppID, Description, Min([Transaction Date]) AS TransactionDate FROM AppProsHist WHERE [De
目前我正在创建规则,该规则应该检查方法是否包含 @Test 和 @TestInfo 注释。如果确实如此,@TestInfo 不应有空参数 testCaseId。 有几种不同的可能方法来填充 testC
是否可以设置参数值,使其在 where 子句中始终结果为 true? 作为示例,考虑一个查询: SELECT name FROM student WHERE class=@parameter; 现在我
我是 JPA 的新手,这是我的查询之一,我有几个参数作为查询的一部分,任何参数都可以为空值 @Query(value = "SELECT ord.purchaseOrderNumber,ord.sal
LOD 参数对 texturelod 取什么值? ?我发现的规范根本没有提到它。它是百分比还是带有百分比的索引值。如果是后者,有没有办法获得纹理具有的 mipmap 数量,以便我能够使用百分比? 最佳
我希望此代码替换现有的 URL 参数“aspid”,但它的作用是在现有的 id 上添加另一个 id。有人可以帮忙吗? $(document).ready(function() { function
在 Spring-boot 项目中,我尝试将 Date 对象作为请求参数传递,并收到此错误: Parameter value [1] did not match expected type [java
在我们的 Jenkinsfile 中,我们有很多参数(参数化构建),在这种情况下,我想检查每个参数是否已切换并对其进行操作。这些参数具有相似的名称,但以不同的小数结尾,因此我想迭代它们以实现此目的。
我的模板之一中有类似于以下内容的内容: 但是 Freemarker 不高兴并给了我: Exception in thread "main" freemarker.core.ParseExceptio
我正在从表单向重定向 servlet 发送一个 post 请求。然后,Servlet 将表单写入其响应 (getWriter) 对象。该表单包含许多隐藏字段。我使用 javascript 提交此表单(
我正在创建一个 JavaScript 组件,我正在根据 jQuery 结果创建该组件的实例,但是,我传递到构造函数中的 DOM 元素虽然在我单步执行调用代码中的循环时已填充,但在传递给构造函数时是未定
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我对 javascript 有疑问。 假设我有这样的 javascript 函数: function show_popup(id) { alert(id); } 编
我目前正在尝试抓取嵌入式 m3u8 url 路径以进行自学。 到目前为止,我设法确定请求会生成带有 m3u8 信息的 json 响应。 例如,https://headlines.yahoo.co.jp
谷歌地图 API 需要这样的参数: NSString *urlString=@"http://maps.google.com/maps?saddr=43.2923,5.45427&daddr=43.4
“parameterValue”是在 Javascript 中的事件上传递的默认参数吗?谁能解释一下这个值从何而来。 Load Ajax content 我发现它在以下文章中使用 - http://w
我有一个 .SWF 电子邮件提交表单。背景颜色通过以下方式设置: `` 并嵌入: `` 是否可以将鼠标悬停在对象或包含的 div 上来更改这些值?即#ffffff 非常感谢! 最佳答案 将 wmode
假设我想用指数函数拟合两个数组x_data_one和y_data_one。为此,我可以使用以下代码(其中 x_data_one 和 y_data_one 被赋予虚拟定义): import numpy
有什么方法可以填充 parameters在基于外部属性文件内容的 Liquibase 变更日志文件中? 例如,我希望能够说: 并将 table.name 的值和
我必须按原样发送参数值 'AbCd/EfgH'。但是 Angular 将 '/' 转义为 %2F。我无法控制 URL。 解决这个问题的最佳方法是什么? 我不想强制 Angular 停止对所有其他 UR
我是一名优秀的程序员,十分优秀!