Scala 不排除带有 => _ 语法的隐式导出
Scala not excluding implicit exports with `=> _` sytnax
根据 this question 和 scala 语言规范,可以使用语法排除导入,例如import java.{xxx => _, _}
.
但是,我发现这不适用于隐式。例如:
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala> import schema.Encoder.{ofGeneric => _, _}
import schema.Encoder.{ofGeneric=>_, _}
根据规范,该成员不可访问:
scala> ofGeneric
<console>:15: error: not found: value ofGeneric
ofGeneric
^
但是,它处于隐式范围,我可以隐式访问它:
scala> :implicits
/* 10 implicit members imported from schema.Encoder */
/* 10 defined in schema.Encoder */
implicit val ofBoolean: Boolean]
implicit def ofConcreteElem[K <: Symbol, A](implicit witness: shapeless.Witness.Aux[K],i
mplicit E: A]): shapeless.labelled.FieldType[K,A]]
implicit val ofDateTime: java.time.OffsetDateTime]
implicit val ofDouble: Double]
implicit def ofElem[K <: Symbol, A, C[_]](implicit witness: shapeless.Witness.Aux[K],imp
licit E: shapeless.Lazy[A]],implicit C: cats.Foldable[C]): shapeless.labelled.FieldType[K,
C[A]]]
implicit def ofGeneric[A, L <: shapeless.HList](implicit G: shapeless.LabelledGeneric.Au
x[A,L],implicit HE: shapeless.Lazy[L]]): A]
implicit def ofHListSeq[K <: Symbol, H, T <: shapeless.HList](implicit witness: shapeles
s.Witness.Aux[K],implicit HE: shapeless.Lazy[shapeless.labelled.FieldType[K,H]]],implicit
TE: shapeless.Lazy[T]]): shapeless.labelled.FieldType[K,H] :: T]
implicit val ofHNil: shapeless.HNil]
implicit val ofInt: Int]
implicit def ofString: String]
scala> implicitly[schema.Encoder[Tuple2[Int,String]]]
res3: schema.Encoder[(Int, String)] = schema.Encoder$$anonfun$ofGeneric@341619cc
^^^^^^^^^
问题:
- 这是控制台中的错误还是语言功能?
- 有没有什么办法可以解决这个问题(即导入除一个以外的所有隐式成员)而不单独指定每个函数?
不确定第一个问题,但解决方法是在您的范围内定义一个具有相同名称的非隐式成员。
来自规范:
The actual arguments that are eligible to be passed to an implicit
parameter of type TT fall into two categories. First, eligible are all
identifiers xx that can be accessed at the point of the method call
... If there are no
eligible identifiers under this rule, then, second, eligible are also
all implicit members of some object that belongs to the implicit scope
of the implicit parameter's type, TT.
The implicit scope of a type TT consists of all companion modules of
classes that are associated with the implicit parameter's type.
所以,这就是说,一旦尝试在本地范围内查找,它就会在伴随对象中查找隐含项。
事实上,这可以通过调用上面的 implicitly[...]
表达式而不是首先导入 anything 来看出;它确实有效,因为这些方法是配套方法的一部分。
根据上面的引述,这应该意味着如果我创建 and/or 将隐式导入作用域,其签名与伴随对象中的那些具有相同的签名,它们将优先,因此无需 'exclude the implicit export' 就像我原来的问题一样。
根据 this question 和 scala 语言规范,可以使用语法排除导入,例如import java.{xxx => _, _}
.
但是,我发现这不适用于隐式。例如:
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala> import schema.Encoder.{ofGeneric => _, _}
import schema.Encoder.{ofGeneric=>_, _}
根据规范,该成员不可访问:
scala> ofGeneric
<console>:15: error: not found: value ofGeneric
ofGeneric
^
但是,它处于隐式范围,我可以隐式访问它:
scala> :implicits
/* 10 implicit members imported from schema.Encoder */
/* 10 defined in schema.Encoder */
implicit val ofBoolean: Boolean]
implicit def ofConcreteElem[K <: Symbol, A](implicit witness: shapeless.Witness.Aux[K],i
mplicit E: A]): shapeless.labelled.FieldType[K,A]]
implicit val ofDateTime: java.time.OffsetDateTime]
implicit val ofDouble: Double]
implicit def ofElem[K <: Symbol, A, C[_]](implicit witness: shapeless.Witness.Aux[K],imp
licit E: shapeless.Lazy[A]],implicit C: cats.Foldable[C]): shapeless.labelled.FieldType[K,
C[A]]]
implicit def ofGeneric[A, L <: shapeless.HList](implicit G: shapeless.LabelledGeneric.Au
x[A,L],implicit HE: shapeless.Lazy[L]]): A]
implicit def ofHListSeq[K <: Symbol, H, T <: shapeless.HList](implicit witness: shapeles
s.Witness.Aux[K],implicit HE: shapeless.Lazy[shapeless.labelled.FieldType[K,H]]],implicit
TE: shapeless.Lazy[T]]): shapeless.labelled.FieldType[K,H] :: T]
implicit val ofHNil: shapeless.HNil]
implicit val ofInt: Int]
implicit def ofString: String]
scala> implicitly[schema.Encoder[Tuple2[Int,String]]]
res3: schema.Encoder[(Int, String)] = schema.Encoder$$anonfun$ofGeneric@341619cc
^^^^^^^^^
问题:
- 这是控制台中的错误还是语言功能?
- 有没有什么办法可以解决这个问题(即导入除一个以外的所有隐式成员)而不单独指定每个函数?
不确定第一个问题,但解决方法是在您的范围内定义一个具有相同名称的非隐式成员。
来自规范:
The actual arguments that are eligible to be passed to an implicit parameter of type TT fall into two categories. First, eligible are all identifiers xx that can be accessed at the point of the method call ... If there are no eligible identifiers under this rule, then, second, eligible are also all implicit members of some object that belongs to the implicit scope of the implicit parameter's type, TT.
The implicit scope of a type TT consists of all companion modules of classes that are associated with the implicit parameter's type.
所以,这就是说,一旦尝试在本地范围内查找,它就会在伴随对象中查找隐含项。
事实上,这可以通过调用上面的 implicitly[...]
表达式而不是首先导入 anything 来看出;它确实有效,因为这些方法是配套方法的一部分。
根据上面的引述,这应该意味着如果我创建 and/or 将隐式导入作用域,其签名与伴随对象中的那些具有相同的签名,它们将优先,因此无需 'exclude the implicit export' 就像我原来的问题一样。