问题标签 [scala-reflect]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
932 浏览

scala - 将类类型传递给 Scala 中的函数

我有一个具有此签名的函数;

通常我会这样称呼它:

我需要从项目类型存储在变量中的地方调用它。

我假设我必须使用反射或返回类型,但我怎样才能传递泛型类型?

0 投票
0 回答
33 浏览

scala - Reflectively determining compatibility of Scala generics

Given a target type (say List[String]) and some object o, the goal is to find a method of o with a return type that is compatible with the target type.

In the absence of generics, one can check this by comparing the target type and the return type of the method using the <:< operator (the scala reflection analog of Java's isAssignableFrom) from scala.reflect.runtime.universe.

This approach does not work in the presence of generics: for example, the return type of the method def getEmptyList[T]: List[T] = Nil does not satisfy List[T] <:< List[String]. How does one determine that the return type of getEmptyList[T] is indeed compatible with List[String]?

0 投票
1 回答
69 浏览

scala - scala中的查找方法通过它的字节码名称反映

有没有办法通过它的字节码名称找到一个方法?

例如,我想println(Object)通过字符串查找引用"_root_.scala.Predef.println(Ljava/lang/Object;)V."

0 投票
2 回答
391 浏览

scala - 伴随对象的实例类

我有以下内容:

我想从一个字符串中实例化 Foo。直到我实现了实例化 Foo,但我无法转换为 C。我正在使用:

现在 foo 是 O.Foo 的一个实例,但它不能转换为 C。

最后一行返回:

0 投票
1 回答
47 浏览

scala - 按名称引用泛型类

考虑我想反序列化 JSON 字符串的情况:

我可以提供我想在编写代码时显式应用函数的类

但是,如果我需要其他类怎么办,我必须在我的代码中提供它,再次编译。我希望我的程序更灵活,它可以采用一个配置文件,其中包含我要使用的类的名称。因此,当需要一个新类时,我只需要编写类定义,将其构建到另一个 jar 文件中,将其放入类路径中,然后重新启动程序。

那么,有可能在scala中做到这一点吗?

0 投票
1 回答
356 浏览

scala - 如何在scala运行时获取类型别名的别名类型?

上面的代码片段将打印TypeTag[Main.INeedDetails]。有没有办法从中提取完整的(Int, String, Unit, Nothing, Float)元组信息TypeTag

0 投票
1 回答
2200 浏览

scala - Spark / scala使用特征中的泛型创建空数据集

我有一个名为的特征,它需要一个类型参数,它的一个方法需要能够创建一个空的类型化数据集。

到目前为止,我还没有让它工作。它抱怨no ClassTag for T,而且value toDS is not a member of org.apache.spark.rdd.RDD[T]

任何帮助,将不胜感激。谢谢!

0 投票
1 回答
54 浏览

scala - 手动创建类型标签

我有一些带有类型标签的 Scala 代码,我需要为 Java 用户提供这些代码。我的问题归结为解决以下问题:

给定两个 Scala 类型标签,如何手动为 Tuple2[ A , B ] 创建类型标签?

如果它有任何区别,我的具体情况会更简单一些:我有一个 A 类型标签,我需要为 Tuple2[ String , A ] 创建一个类型标签

0 投票
1 回答
93 浏览

scala - 在 scala 中,Map[_,_] 和 scala.collection.immutable.Map[_,_] 怎么可能有不同的 TypeTag?

它们指的是同一件事,但是当我比较 2 种类型标签时:

我有:

这怎么会在打字语言中发生?我如何使它们相同?

更新:对于 List 这更令人困惑:

它们是完全一样的!然而反射选择忽略它。

0 投票
0 回答
241 浏览

scala - 与 ClassTag 或 Manifest 的模式匹配:代码异味?

我想知道使用在运行时通过 ClassTag 或 Manifest 已知的泛型类型是否实际上不是代码异味,从而导致不可预测的结果。

以下是可能发生的情况的示例:

我确实理解原因(当提供 BaseTrait 和 OtherTrait 时,Classtag 仅捕获 BaseTrait)。我也明白 Manifest 具有相同的行为,并且 TypeTag 无法拯救我,因为您无法在运行时根据 TYpeTag 检查实例。

那么问题是:我们不应该期望编译器抛出警告吗?还是我错过了什么?(我想答案是'你错过了一些东西')