- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
斯卡拉 2.12.6,无形 2.3.3。
我有很多非常深的大型模型(案例类)。我使用 shapeless 来帮助使用/操作这些模型,并且还使用像 circe 这样大量使用 shapeless 的库。
这导致我的编译时间在 phase typer
期间大量增加。 scalac 的一部分。
基于一些谷歌搜索,看起来无形是罪魁祸首,但我似乎无法找到任何关于如何改进这一点的具体提示。
有人建议,因为我为相同的模型多次解析 HList 隐式(因为有多个库),所以我应该“缓存它们” - 但是我不确定如何确切地确定要缓存的内容。
给出类似的东西:
case class MyModel(value: String) extends AnyVal
case class MyOtherModel(value: Int) extends AnyVal
case class MyRootModel(myModel: MyModel, myOtherModel: MyOtherModel)
MyModel
/
MyOtherModel
和
MyRootModel
?
最佳答案
您可以缓存 Shapeless 的 LabelledGeneric
像这样的实例:
import shapeless.{LabelledGeneric, the}
case class MyModel(value: String) extends AnyVal
case class MyOtherModel(value: Int) extends AnyVal
case class MyRootModel(myModel: MyModel, myOtherModel: MyOtherModel)
object MyModel {
implicit val generic = the[LabelledGeneric[MyModel]]
}
object MyOtherModel {
implicit val generic = the[LabelledGeneric[MyOtherModel]]
}
object MyRootModel {
implicit val generic = the[LabelledGeneric[MyRootModel]]
}
LabelledGeneric
重复(在这种情况下是一千次):
object Test {
def foo0 = {
LabelledGeneric[MyRootModel]
LabelledGeneric[MyRootModel]
// repeat 98 more times...
}
def foo1 = {
LabelledGeneric[MyRootModel]
LabelledGeneric[MyRootModel]
// repeat 98 more times...
}
// and so on through foo9
}
Test.scala
包含
Test
的文件、案例类定义和缓存实例在大约 3 秒内编译。如果我们注释掉
generic
定义,它需要超过 12 秒。这当然很不科学,但令人鼓舞。
implicit
并不是一个好主意。没有类型注释的定义,您可以通过编写如下内容来避免对缓存实例执行此操作:
import shapeless.{LabelledGeneric, TypeOf, cachedImplicit}
case class MyModel(value: String) extends AnyVal
case class MyOtherModel(value: Int) extends AnyVal
case class MyRootModel(myModel: MyModel, myOtherModel: MyOtherModel)
object MyModel {
implicit val generic: TypeOf.`LabelledGeneric[MyModel]`.type = cachedImplicit
}
object MyOtherModel {
implicit val generic: TypeOf.`LabelledGeneric[MyOtherModel]`.type = cachedImplicit
}
object MyRootModel {
implicit val generic: TypeOf.`LabelledGeneric[MyRootModel]`.type = cachedImplicit
}
TypeOf
不过,这是一些奇怪的魔法,老实说,当我需要这样的东西时,我刚刚使用了
the
方法。
关于scala - 减少使用 shapeless HList 时的编译时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56233806/
有 (Some(1), 2, Some(3)) 我希望得到 (Some(1), Some(2), Some(3)) 使用无定形可以做到吗? 最佳答案 是的,无形可以做很多事情! import sh
我正在开发一个用于经济模型的小型库,用于检查实体的单位,使用类型,例如而不是 val apples = 2.0我们写 val apples = GoodsAmount[KG, Apples](2.0)
据我了解,依赖类型允许您不指定输出类型: 例如,如果您有一个类型类: trait Last[In] { type Out } 然后你可以在不指定输出类型的情况下召唤一个实例: implicitly
我正在考虑做类似 Safely copying fields between case classes of different types 的事情但字段重新排序,即 case class A(foo
一旦出现类型参数,shapeless 中的类型不等式似乎就不安全了。 例如下面的代码编译 def someMethod[T](in : T) = { implicitly[T =:!= Strin
我仍在努力了解 Shapeless(在较小程度上,Scala!)并且我一直在编写一些简单的代码来为案例类生成随机实例数据 - 主要基于此处的指南:http://enear.github.io/2016
shapeless 是否提供了一个类型类/宏来调用一个 HList,其中包含一个 case 类的所有参数的默认值? 假设我们有这个案例类: case class User(name: String,
Shapeless 有一个整洁的类型类 derivation mechanism它允许您定义类型类并获得任何类型类的自动派生。 要使用派生机制作为类型类的用户,您将使用以下语法 import MyTy
昨天我和几个同事在探索Shapeless,我们决定写一个玩具方法来向case类的第一个参数添加一个,当该参数是Int时。 : def addOneToCaseClass[C, H <: HList,
给定一组具有两个不同子集的 Adt 例如: sealed trait Domain[Y] sealed trait Command[Y] extends Domain[Y] sealed trait
给定一个案例类 A我可以使用以下代码段使用 Shapeless 提取其字段名称: val fieldNames: List[String] = { import shapeless._ imp
假设我有几个功能: val f1: Int => String val f2: (Int, Int) => String val f3: (Int, Int, Int) => String def f
鉴于我的类型为 Int :+: Int :+: String :+: CNil , 有没有简单的方法把它变成Int :+: String :+: CNil ? 最佳答案 这取决于您所说的“容易”是什么
在我正在开发的 Play 应用程序中,我正在尝试改进我们处理标志的系统,其中一些是当用户通过链接导航我们的应用程序时的持久选项。我想使用 Shapeless 将选项的定义映射到它的值,并仅从标记为要传
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我可以编写一个简单的递归多态函数: object simpleRec extends Poly1 { implicit def caseInt = at[Int](identity) impl
我想将一个类的更新实例合并到一个基本实例中,如果该字段在基本实例中为“空”,则选择更新实例的字段而不是基本实例。下面的示例合并了 base 和 update: case class Foo(a: Op
很容易按类型过滤无形状的 HList: val hlist = 1 :: 2 :: "3" :: true :: false :: HNil hlist.filter[Int] 但是我怎样才能制作我的
是否可以使用 shapeless 将 1 个对象转换为另一个对象 做一些小的转换,比如将 Option[T] 转换为 T (无需为每个类手动定义映射) 忽略缺失的字段 进口无形。_ 导入 shapel
有谁知道如何使用 Shapeless 进行此测试。 package net.jtownson.swakka.jsonschema import org.scalatest.FlatSpec impor
我是一名优秀的程序员,十分优秀!