问题标签 [type-inference]

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 投票
3 回答
1216 浏览

generics - Why can't Scala infer the type parameter in this example?

Suppose I have two classes, Input and Output, which are designed to be connected to each other. Output produces values of some type, and Input consumes them.

It's okay if an Input and Output pair don't operate on the same kind of value as long as the Input type parameter is a supertype of the Output type parameter. Note that the type parameter in both classes is invariant; in the real versions it is used in both co- and contravariant positions.

I have a connect method elsewhere which sets up a link between an Input/Output pair:

If I call this method as below, I get a type error:

The error is:

I can resolve this by writing out the type parameter (in this case, I would write connect[String], but I think the compiler should be able to figure this out for me. How can I change the connect method so that the type parameter is inferred automatically?


Edit: For now, I've made connect a method of Output so it gets the type parameter automatically. This also has the added benefit that I can use the infix notation out connect in, but the design feels a little awkward.

I am still interested in why the compiler exhibits this behavior. I feel like it should be able to infer the type parameter. Is this actually working as specified?

0 投票
3 回答
1795 浏览

f# - 无限类型(又名递归类型)在 F# 中是不可能的吗?

我在 Twitter 上与Sadek Drobi聊天时被告知 F# 似乎不支持 Infinite Types。事实证明,在 C# 中,您可以执行以下操作:

然而,在我们双方的一些实验之后,我们确定在 F# 中相同的内容似乎是不可能的,无论是隐式的还是显式的。

显式:

错误 FS0191:此类型定义涉及通过缩写、结构字段或继承关系的直接循环引用。

隐式:

类型不匹配。期待一个'b,但给定一个'a->'b。当统一 ''b' 和 ''a -> 'b' 时,结果类型将是无限的。

当然,这些是故意简单的示例。

我想知道我是否弄错了。也许我错过了某种必要的注释?

0 投票
2 回答
1469 浏览

type-inference - 类型推断的限制是什么?

类型推断的限制是什么?哪些类型的系统没有通用推理算法?

0 投票
5 回答
51855 浏览

scala - Scala:如何定义“通用”函数参数?

我现在正在尝试学习 Scala,在 Haskell 方面有一点经验。对我来说很奇怪的一件事是 Scala 中的所有函数参数都必须使用类型进行注释——这是 Haskell 不需要的。为什么是这样?试着把它作为一个更具体的例子:一个 add 函数是这样写的:

但是,这仅适用于双精度数(好吧,由于隐式类型转换,整数也可以工作)。但是,如果您想定义自己的类型来定义自己的+运算符怎么办。您将如何编写适用于任何定义+运算符的类型的 add 函数?

0 投票
2 回答
1026 浏览

haskell - Haskell:类型推断和函数组合

这个问题的灵感来自另一个问题的答案,表明您可以使用定义为的函数从列表中删除每个出现的元素:

用铅笔和纸从 和 的类型中计算出来,filter函数的类型为(/=)(.)

这正是您根据其合同所期望的。但是,使用 GHCi 6.6,我得到

除非我明确指定类型(在这种情况下它可以正常工作)。为什么 Haskell 会为函数推断出如此特定的类型?

0 投票
2 回答
963 浏览

haskell - 类型推导在 Haskell 中是如何工作的?

我试图通过学习 Haskell 来拓宽我的思维。

我自己做的作业是构建一个时钟滴答发生器,它会给我泊松分布的间隔,最终结果(经过长时间的斗争,我承认)是这样的:

但是有两件事(至少)我不明白:

为什么我需要“ Ord r”以及“ Floating r”?(我本来期望某种自动继承:“Floating”意味着“Ord”。)

隐含的类型定义“ rvalue :: Float”是通过什么途径实现的?在 GHCi 中,我得到了我所期望的:

rvalue是一门松散的大炮,我必须系好:

请对 Haskell n00b 温柔一点。

0 投票
4 回答
4976 浏览

c# - C# 中带有类型参数的泛型类型

我不认为这可以在 C# 中完成,但发布这个只是为了确保。这是我的问题。我想在 C# 中做这样的事情:

或者

但这当然行不通。尽管这应该不是问题,因为类型 t 在编译时已解决。我知道这可以通过反射来解决,但是由于在编译时使用反射就知道类型是多余的。

0 投票
2 回答
927 浏览

c# - 为什么在循环通过 Visio Masters 集合时 C#“var”关键字不起作用?

我正在使用 Visio API 循环遍历a集合中的每个Microsoft.Office.Interop.Visio.Master对象。Microsoft.Office.Interop.DocumentMasters

当我var如下使用时,编译器只识别master为类型object并抛出错误:

相反,我必须这样做才能使其工作:

为什么会这样?var通常我在 foreach 循环中使用没有问题。

编辑:我在使用该Open方法时得到的错误master是:

“object”不包含“Open”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“Open”(您是否缺少 using 指令或程序集引用?)

0 投票
2 回答
678 浏览

java - JSTL foreach 和智能感知

使用这样的代码:

IntelliJ Idea 能够推断出 ForEach 循环中“客户”变量的类型属于“客户”类(假设客户类似于 List<Customer>)。如果我重构 java 客户类并将 getName 更改为 getFullName,它会将 jstl 更改为读取 ${customer.fullName}。

我下载了standard.jar 的源代码,但我看不出这是如何真正做到的。我知道您应该能够使用 tei 类发出类型信息,但 Jakarta Taglibs 中的 TEI 类不这样做。有人知道我错过了什么吗?

(我正在尝试制作自己的 foreach,但除非我能获得相同级别的支持,否则不会这样做,但我只是不知道该怎么做..)

0 投票
5 回答
1326 浏览

scala - Scala 中的简单类型推断

我一直在研究 Scala 中的类型推断,关于为什么在某些情况下必须显式声明表达式/方法返回类型,我想更好地理解一些事情。

显式return声明

示例(如果return省略关键字则有效):

为什么我不能在不声明返回类型的情况下使用显式类型的参数作为返回值?这不仅适用于直接参数引用,也适用于任何“类型可推断”的表达式。

方法重载

joiner示例(添加第二种方法时编译失败):