class PEControl[T <: Data : Arithmetic](accType: T),这是来自 riscv-gemmini 的类定义。的Data类型是在凿的基本数据类型,Arithmetic提供了关于某些算术运算Data,和abstract class Arithmetic[T <: Data]。
用于的语法是<: Type : Type什么,这是什么意思?我发现语法是TypeParamBounds ::= TypeBounds {‘:’ Type}从这里调用的。哪里可以详细了解一下,谢谢。
也许我在spray-json中发现了一个bug.当我试图获取具有自身类型字段的对象的json时,我得到Null Pointer Exception.例如:
case class TestItem(subitems: Option[List[TestItem]])
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val testItemFormat: RootJsonFormat[TestItem] = jsonFormat(TestItem, "subitems")
}
import MyJsonProtocol._
object TestNPE {
def main(args: Array[String]) {
val subitems = List(TestItem(None))
val item: TestItem = TestItem(Option(subitems))
val jsonAst = item.toJson
val json = jsonAst.prettyPrint
println(json)
}
}
Run Code Online (Sandbox Code Playgroud)
而call-stack就是这个
Exception in thread "main" java.lang.NullPointerException
at spray.json.PimpedAny.toJson(package.scala:40)
at spray.json.CollectionFormats$$anon$1$$anonfun$write$1.apply(CollectionFormats.scala:26)
at spray.json.CollectionFormats$$anon$1$$anonfun$write$1.apply(CollectionFormats.scala:26)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.immutable.List.foreach(List.scala:309)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at spray.json.CollectionFormats$$anon$1.write(CollectionFormats.scala:26)
at spray.json.CollectionFormats$$anon$1.write(CollectionFormats.scala:25)
at spray.json.PimpedAny.toJson(package.scala:40)
at …Run Code Online (Sandbox Code Playgroud) 如何指定我需要例如ClassTag和T的订购?
例
def sort[T: ClassTag <and> Ordering](future: Future[Seq[T]]): Future[Seq[T]]
通常,我首先在同一个文件中写一个case class然后是同伴object,就在下面.但是当试图导入implicit同伴中的声明时,我被迫切换声明的顺序(显然我不想要).克服这种情况的建议做法是什么?
对于具体情况,以下代码不起作用:
object SomeLib {
def doSomething[T : List](t: T) = "meh"
}
case class FooWorker(x: String) {
import FooWorker._ // bring the implicit in scope for doSomething
def then(event: String) : FooWorker = {
copy(x = SomeLib.doSomething(event)) // requires implicit
}
}
object FooWorker {
implicit val list = List("a", "b", "c")
}
Run Code Online (Sandbox Code Playgroud)
但如果我object FooWorker在case class FooWorker它确实工作之前宣布.我正在使用Scala 2.11.6和SBT进行测试.非常感谢!
在scala抽象类中,如果你想定义一个上下文绑定,你可以简单地在参数中使用,例如[T:ClassTag],但是这在trait中是不可能的:
trait Foo[T: ClassTag]
Error:(11, 35) traits cannot have type parameters with context bounds `: ...' nor view bounds `<% ...'
trait Foo[T: ClassTag]
^
Run Code Online (Sandbox Code Playgroud)
如果你定义:
trait Foo[T] {
implicit def ctg: ClassTag[T] = implicitly[ClassTag[T]]
}
object Bar extends Foo[Int]
Run Code Online (Sandbox Code Playgroud)
那么任何在 Bar 中读取 ctg 的尝试都会触发 StackOverflowError,因为隐式参数变为尾递归。
那么允许 ctg 定义在自动将子类暴露给上下文绑定的特征中的最佳方法是什么?
对于
sealed trait User {...}
sealed trait Trader extends User {...}
trait AuthObject
trait AuthUserObject {
def authorize[U <: User](u: U): Boolean
}
trait AuthTraderObject extends AuthUserObject {
def authorize[T <: Trader](t: T): Boolean
}
object HasPaidTax extends AuthTraderObject {
def authorize[T <: Trader](t: T): Boolean = t.hasPaidTax
}
Run Code Online (Sandbox Code Playgroud)
这不构建.错误:
错误:(15,7)覆盖方法在特征[U <:users.User](u:U)布尔值的特征AuthUserObject中授权; 方法授权具有不兼容的类型def authorize [T <:Trader](t:T):Boolean ^
我需要限制AuthTraderObject到Trader,因为只有交易者缴纳税款的用户.请问,这种覆盖怎么可能?
我正在学习 Scala 中的上下文绑定。
在下面的代码中,我对整数参数调用乘法运算符。但它会出错。'a' 被视为类型参数;但实际上并不符合我的理解。有人可以帮忙吗。
scala> class Sample[T]
defined class Sample
scala> def method[Int:Sample](a:Int) = a * a
<console>:12: error: value * is not a member of type parameter Int
def method[Int:Sample](a:Int) = a * a
Run Code Online (Sandbox Code Playgroud)
谢谢!