- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设你有像下面这样的案例类
case class Test1(a:String,b:Int,c:Char)
case class Test2(a:String,b:Int)
val test1 = Test1("first",2,'3')
val test2 = Test2("1st",20)
.copy
方法(或其他方式),将 Test2 中的变量应用于 Test1,类似于
val test3 = test1.copy(test2) //Note this isn't valid scala code
// Result should be ("1st",20,'3')
最佳答案
在 shapeless 2.0.0 中,这可以像这样完成,
scala> import shapeless._
import shapeless._
scala> case class Test1(a: String, b: Int, c: Char)
defined class Test1
scala> case class Test2(a: String, b: Int)
defined class Test2
scala> val test1 = Test1("first", 2, '3')
test1: Test1 = Test1(first,2,3)
scala> val test2 = Test2("1st", 20)
test2: Test2 = Test2(1st,20)
scala> val test1Gen = Generic[Test1]
test1Gen: ... = $1$$1@6aac621f
scala> val test2Gen = Generic[Test2]
test2Gen: ... = $1$$1@5aa4954c
scala> val test3 = test1Gen.from(test2Gen.to(test2) :+ test1.c)
test3: Test1 = Test1(1st,20,3)
LabelledGeneric
来解决这个问题。 .
LabelledGeneric
将 case 类值映射到无形的可扩展记录,除了捕获字段值的类型外,还通过相应 Scala 的单例类型对类型中的字段名称进行编码
Symbol
.通过一些额外的基础设施(我将很快添加到 shapeless 2.1.0),这允许我们使用最少的样板安全地在案例类之间进行映射,
import shapeless._, record._, syntax.singleton._, ops.hlist.Remove
/**
* This will be in shapeless 2.1.0 ...
*
* Permute the elements of the supplied `HList` of type `L` into the same order
* as the elements of the `HList` of type `M`.
*/
trait Align[L <: HList, M <: HList] extends (L => M) {
def apply(l: L): M
}
object Align {
def apply[L <: HList, M <: HList]
(implicit alm: Align[L, M]): Align[L, M] = alm
implicit val hnilAlign: Align[HNil, HNil] = new Align[HNil, HNil] {
def apply(l: HNil): HNil = l
}
implicit def hlistAlign[L <: HList, MH, MT <: HList, R <: HList]
(implicit
select: Remove.Aux[L, MH, (MH, R)],
alignTail: Align[R, MT]): Align[L, MH :: MT] = new Align[L, MH :: MT] {
def apply(l: L): MH :: MT = {
val (h, t) = l.removeElem[MH]
h :: alignTail(t)
}
}
}
/**
* This, or something like it, will be in shapeless 2.1.0 ...
*
* Utility trait intended for inferring a field type from a sample value and
* unpacking it into its key and value types.
*/
trait Field {
type K
type V
type F = FieldType[K, V]
}
object Field {
def apply[K0, V0](sample: FieldType[K0, V0]) =
new Field { type K = K0; type V = V0 }
}
object OldWineNewBottle {
case class From(s1: String, s2: String)
case class To(s2: String, i: Int, s1: String)
val from = From("foo", "bar")
val fromGen = LabelledGeneric[From]
val toGen = LabelledGeneric[To]
// Define the type of the i field by example
val iField = Field('i ->> 0)
val align = Align[iField.F :: fromGen.Repr, toGen.Repr]
// extend the fields of From with a field for 'i', permute into
// the correct order for To and create a new instance ...
val to = toGen.from(align('i ->> 23 :: fromGen.to(from)))
assert(to == To("bar", 23, "foo"))
}
关于scala - 在不同类型的案例类之间安全地复制字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192760/
在我的 java 代码中,我做了类似的事情: int sleep = 0; sleep(sleep); sleep++; 被bos指出不好。它不能在 php 中正常工作。 在 java 中使用上述代码
我有一个程序使用第 3 方库进行一些计算,我在与主应用程序不同的线程上启动该程序。不幸的是,此计算可能需要很长时间,并且不提供进度更新和取消的接口(interface)。 为了拥有这样的界面,我想创建
C++ 是否有任何等效于 python 的函数 os.path.join?基本上,我正在寻找将文件路径的两个(或多个)部分组合在一起的东西,这样您就不必担心确保这两个部分完美地结合在一起。如果它在 Q
我正试图站起来(非商业)web application使用 neo4j Community 3.5.2 作为后端。 应用程序以两种方式与图形交互: 服务器端(安全的)用 flask 编写新的节点和关系
我正在开发一个将有许多外部用户的在线应用程序。至于现在,我的连接方法是为所有用户托管一个中央数据库,而他们从自己的服务器文件连接。 方法: PHP 连接文件(托管在他们的服务器上;文件由我提供) >>
我创建了一个将所有事件通知代码转换为字符串的函数。真的很简单。 我有一堆常量,比如 const _bstr_t DIRECTSHOW_MSG_EC_ACTIVATE("A video window i
我想将(附加)信息从过滤器传递到资源。我目前尝试这样做的方式是,在 Filter 中: getContext().getAttributes().put("additionalInformation"
我想计算转换系数。为此,我必须除以例如的最大值。 ushort 为 uchar 的最大值。 我想通过将参数传递给函数或类型名来动态地执行此操作。然后我想选择最大值并执行计算。 有两个问题: 如何动态选
我希望我的用户在用 Java 请求列表时能够编写自己的过滤器。 选项 1) 我正在考虑将 JavaScript 与 Rhino 结合使用。 我将用户的过滤器作为 javascript 字符串获取。然后
(安全地)提供来自不同域的图像是否符合 PCI 标准?我搜索了 PCI DSS 2.0 PDF,但没有找到任何引用资料。 最佳答案 图像不符合 PCI 合规性。 PCI DSS covers the
我们正在将 spring 和 hibernate 用于 web 应用程序:该应用程序有一个购物车,用户可以在其中放置商品。为了保存不同登录名之间要查看的项目,购物车中的项目值存储在表中。提交购物车时,
我正在为多个客户创建一个具有电子商务元素的 Rails 应用程序 - 我希望这些客户能够在管理区域中指定计算运费的公式;因为方法可能不同。 让我们假装一下,我允许他们输入 ruby 代码,然后我稍
我正在 Eclipse 中开发一个 Java 项目,使用 Maven 构建和管理依赖项。该项目分布在 5 个 Eclipse 项目中,其中一个是父 POM。我正在研究基于另一个团队实现的更复杂服务器的
我想在 ADO.NET 数据服务中存储每线程数据。在线程特定的静态变量上使用 ThreadStatic 属性是否安全,或者我会遇到问题吗?我担心的是,我的 ThreadStatic 变量在请求完成并且
Stackoverflow 上至少有一篇与此主题相关的帖子:Generate password in python 你会发现这个主题甚至在 PEP 中也受到了一些批评。这里提到:https://www
对于我工作中的一个项目,我需要创建一个独立的 Python 安装(来自源代码)。然而,完整的目录占用大约 90MB 的磁盘空间,虽然不多,但太多了,无法一遍又一遍地复制。 我可以从自定义 python
例如,我有一张学生表,我有一本 Python 字典 mydict = {"fname" : "samwise", "lname" : "gamgee", "age" : 13} 我怎样才能安全地生成一
我经常在代码中遇到使用 memset 手动零初始化的 POD 结构,如下所示: struct foo; memset(&foo, 0, sizeof(foo)); 我检查了 C++11 标准,它说:“
我是一名优秀的程序员,十分优秀!