首页 > 解决方案 > 包含“$eq”的标记泛型

问题描述

Shapeless 2.3.3LabelledGeneric在以下案例类上运行时返回一个奇怪的结果:

scala> case class Foo(`$eq`: Int)
defined class Foo

scala> LabelledGeneric[Foo]
res0: shapeless.LabelledGeneric[Foo]{type Repr = Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("=")],Int] :: shapeless.HNil} = shapeless.LabelledGeneric$$anon$1@1ac7dbd3

请注意,返回的标签实际上是=而不是$eq.

这种行为是无形的怪癖还是其他什么?是否有其他标识符名称会导致奇怪的行为?

我需要这个来进行 MongoDB 序列化,这对我来说是一个非常头疼的问题......

标签: mongodbscalashapeless

解决方案


根据 Alexey 在 Q 中的评论,这是 Shapeless 中的一个“功能”,因为 scala/java 互操作的工作方式。后者在Daniel Spiewak 的“运算符和超载”下的印迹文章中进行了解释。

根据博客,受此影响的运营商列表是:

op  Compiles To
=   $eq
>   $greater
<   $less
+   $plus
-   $minus
*   $times
/   div
!   $bang
@   $at
#   $hash
%   $percent
^   $up
&   $amp
~   $tilde
?   $qmark
|   $bar
\   $bslash
:   $colon

推荐阅读