问题标签 [reification]
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.
generics - Kotlin reified type parameter doesn't smart cast
I was experimenting with setting uninitialized values and was trying to get the following to work. This is mostly a curiosity in the power (and limitations) of reified generics.
I was attempting to provide default values for optional parameters of data classes.
When when
includes is Type
clauses, Kotlin has smart casting. In this example, smart casting isn't kicking in so this will not compile.
Any ideas to accomplish something similar?
prolog - 如何动态重写 CLP(FD) 约束以帮助具体化
我的问题与 CLP(FD) 程序中的具体化问题有关的这篇(现在已有 1 年的)帖子有关:链接
我提供给 SWI 引擎的 prolog 文件是即时以编程方式编写的,基于用户可以使用另一个应用程序添加/编辑/删除的一些数据(之后调用引擎的相同)。
我想检测一些特殊情况来动态重写约束以帮助“引擎”找到可能的最小域。一个例子 :
如果我打电话constr(3,Y,Z).
,我得到Z #= 1
or Z #= 2
。
一种解决方案是自动替换最后两行
有了这个我得到Z #= 1.
问题是我可以拥有这个:
如何检测到这一点?Prolog 引擎可以帮助我做到这一点吗?我以为我可以通过具体化变量以编程方式替换每个正确的操作数:
但在那种情况下,会出现同样的“物化”问题。
谢谢你的帮助。
generics - 过滤没有反射或强制转换的泛型类型
在 Kotlin 中有一种有限形式的具体化泛型。有没有办法使用具体化来过滤泛型类型而不使用getClass()
或as
或任何奇怪的注释,即。仅仅通过使用is
关键字?例如,我有以下结构:
这输出:
我希望它输出以下内容。有没有办法获得:
如果我们查看 的源代码filterIsInstance(...)
,Kotlin 做了一些棘手的事情来规避类型擦除,但仍然不起作用:
如果这在 Kotlin 中是不可能的,是否有任何语言(JVM 或非 JVM)可以让我执行以下操作:
java - 如何在 Kotlin 的实例字段中存储具体类型数据?
我目前正在为一个库编写一个 DSL,我想使用这样的具体类型参数来提供类型元数据:
我的问题是我只能在函数中使用reified
关键字,inline
而在inline
函数中我不能使用这样的实例字段:
因为我收到一个错误:
公共 API 内联函数无法访问非公共 API 'private final val consumer...
到目前为止,我似乎无法在最有用的地方使用具体类型参数。有解决方法吗?
java - How can I call Kotlin methods with reified generics from Java?
I have the following method in Kotlin:
If I try to call this from Java like this:
OR like this:
I get the following error:
java: foo() has private access in MyClass
How can I call the foo
method from Java?
java - Kotlin generics Array results in "Cannot use T as a reified type parameter. Use a class instead" but List does not
I have an interface that contains an array (or list) of T and some metadata.
If I write the simplest implementation of the interface, I get a compile error on the emptyArray()
: "Cannot use T as a reified type parameter. Use a class instead."
However, if I change both the interface and the implementation to a list, I have no compile-time issues:
I suspect there is some interesting lesson in Kotlin generics inside my issue. Can anyone tell me what the compiler is doing under the hood and why Array fails but List does not? Is there an idiomatic way to make the Array implementation compile in this context?
Bonus question: The only reason I reached for Array over List is that I often see Kotlin developers favor Arrays. Is this the case, and if so, why?
rdf - 下载 Wikidata 单一实体 - 真实
我想下载单个实体的 Wikidata。我知道我可以使用 URL 来实现,例如:
https://www.wikidata.org/wiki/Special:EntityData/Q42.rdf
将为我提供RDF 格式的Douglas Adams的 Wikidata。
但是这个数据是完整的,意味着完整的元数据,如限定符和引用。我只对原始数据感兴趣。
实际上我正在研究 RDF Reification,为此我需要一些可以测试我的程序的示例非 RDF 数据(如真实的 Wikidata)。
我不希望下载整个 Wikidata 转储(我知道它们以真实格式提供)。
generics - 如何表示 owl 泛型,例如参数类?
我知道猫头鹰没有通用的参数类:POTUS(startyear,endyear)。代表这一点的最佳方式是什么?是我使用实例和具体化而不是类的唯一选择吗?我的模型必须能够最好地使用 DL 而不是 SPARQL 来回答给定年份的总统是谁(以及许多概念)。
java - 如何将通用接口传递给它被具体化的方法?
我有一个通用接口
以及它的几个实现,通过普通的类,比如
和匿名类(见下文)。现在我想测试这些实现:
TestClass1
问题是编译器由于从泛型接口转换到其具体版本(我需要与具体类一起使用并TestClass2
代替泛型类型变量T
)而发出一系列错误和警告。是否可以完全避免这些警告?如果不是(即,如果它们只能被压制),是否有由此产生的任何陷阱?
generics - Kotlin 中的协议实现可识别的推断类型
我正在尝试创建一个系统,其中协议(或抽象类)的实现满足该协议的最低要求,但仍然可以访问所提供解决方案附带的独特功能。让我用一个例子来说明:
现在要破解这个问题,我一直在研究泛型和具体化函数,但我无法想出以表达方式工作的东西。
我应该注意,我的实现必须满足多个要求。我可以通过在协议上使用泛型来完成这项工作。
这会起作用,但是有多个要求它很快看起来很荒谬,我觉得我错过了一些东西。属性的泛型不起作用,除非我在下一个示例中遗漏了一些东西。:
然后我的具体方法看起来也不那么漂亮:
在这一点上,我不完全确定我是否不知道该怎么做,或者是否不可能。感觉这是可能的,我想知道你们中是否有人能指出我如何解决它的正确方向。
或者也许我想要做的事情甚至没有意义,但即使是我想听到的,因为它对我设想的设置确实有意义。
提前致谢。
第一次回复后编辑: 我打算在构建阶段之后访问 favoriteFood。将可变的 favoriteFood 变成了一个更符合目标的 val。