type-inference 相关问题

类型推断是使用类型系统定义的规则自动推断程序类型的过程。


我有以下f#示例。它编译。但是,无论我尝试什么,我都无法进行编译。编译器立即告诉我,

显示我尝试的内容(其中一些不同的组合)。我也尝试在这里和那里使用<^I>Model

回答 1 投票 0


为什么$ {CELL u} abc提取了“ Hello”? 也是,如果我将模式更改为$ {CELL u} C,则结果将变为“ HelloAb”。打字稿如何确定在哪里停止推断u? 我正在努力理解您的工作方式。有人可以澄清吗? typecript检查以查看类型参数是否扩展了模板字体类型。这是一个没有T:的更简单的示例 infer 这里,您告诉Typescript检查是否以type CheckPrefix<T> = T extends `${string}abc` ? true : never; 结尾。您可以使用此模式来检查子字符串。例如,您可以使用模板字面类型T检查字符串字符类型是否包含abc并以${string}abc${string}xyz结尾。请注意,一个空字符串(abc)是有效的类型,因此xyz仍然满足""。 发起 typecript检查是否可以分为某些字符串"abcxyz",然后是字面的${string}abc${string}xyz。由于T以U结尾,typeScript为您提供条件上的真实分支,即"abc",其余的。这就是为什么从"helloabc"提取"abc"的原因。 typescript检查类型的字面部分是否(例如U或T完全匹配"hello"。当发生类型的下一个字面部分时,编译器“停止”推断${infer U}abc当您更改时。类型为"abc"的字面部分,类型的“静止”是thim。 这也值得注意的是,您可以使用"c"inides。例如: T 模板字面类型有很多有关此信息的信息,值得一读。

回答 1 投票 0



标题差的apologies。我很感谢您在制作更好的帮助方面的帮助,但我什至不知道如何形容我的问题。

说我要举办一个实现这两个接口的课程。 record A<A1, A2> (A1 a1, A2 a2) implements M<A1, A2>, F<A1, A2> {} ,很酷。现在,我可以在任何地方使用A或M.。 现在这是我的问题。我正在尝试执行以下操作,并因编译器错误而失败。 F,所以一切都编译了。但是,如果我取消返回并替换异常,我会得到以下汇编错误。 public class DoublyNestedGenerics { interface M<K, V> {} interface F<I, O> {} record A <A1, A2> (A1 a1, A2 a2) implements M<A1, A2>, F<A1, A2> {} private static < D1, D2, D extends M<D1, D2> & F<D1, D2> > D canItWork(final D1 d1, final D2 d2) { // return new A(d1, d2); throw new UnsupportedOperationException(); } } ,很有意义。让我改变回报为此。 DoublyNestedGenerics.java:28: warning: [rawtypes] found raw type: A return new A(d1, d2); ^ missing type arguments for generic class A<A1,A2> where A1,A2 are type-variables: A1 extends Object declared in record A A2 extends Object declared in record A 按压编译给我以下编译器错误。 return new A<>(d1, d2); 我没有完全理解错误,因此我决定简化问题。 I更改了使用一些硬编码类型的方法,而不仅仅是DoublyNestedGenerics.java:28: error: incompatible types: cannot infer type arguments for A<> return new A<>(d1, d2); ^ reason: no instance(s) of type variable(s) A1,A2 exist so that A<A1,A2> conforms to D where A1,A2,D,D1,D2 are type-variables: A1 extends Object declared in record A A2 extends Object declared in record A D extends M<D1,D2>,F<D1,D2> declared in method <D1,D2,D>canItWork(D1,D2) D1 extends Object declared in method <D1,D2,D>canItWork(D1,D2) D2 extends Object declared in method <D1,D2,D>canItWork(D1,D2) 1 error 和D1。这是新方法的样子。 D2 交出评论并按编译给我以下编译器错误。 private static < D extends M<Integer, Integer> & F<Integer, Integer> > D canItWorkAttempt2(final Integer d1, final Integer d2) { // return new A<>(d1, d2); throw new UnsupportedOperationException(); } ,小得多,因此更容易解析。 对我来说很突出的事情是说DoublyNestedGenerics.java:44: error: incompatible types: cannot infer type arguments for A<> return new A<>(d1, d2); ^ reason: no instance(s) of type variable(s) A1,A2 exist so that A<A1,A2> conforms to D where A1,A2,D are type-variables: A1 extends Object declared in record A A2 extends Object declared in record A D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer) 1 error 。 well这是不对的 - 应该是A1 extends Object declared in record A,而不是。也许推论需要一些帮助。因此,我将回报更改为此。 Integer 导致以下编译器错误。 Object杂志!我现在开始怀疑,为了确认它们,我决定大大简化我的问题。我创建了以下类。 return new A<Integer, Integer>(d1, d2); 没有问题。美丽,让我们尝试更改返回类型以使用此类型。这是我更改了返回的内容。 DoublyNestedGenerics.java:44: error: incompatible types: A<Integer,Integer> cannot be converted to D return new A<Integer, Integer>(d1, d2); ^ where D is a type-variable: D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer) 1 error 高希望,我按编译。 record N (Integer n1, Integer n2) implements M<Integer, Integer>, F<Integer, Integer> {} 非常令人失望。但是还有其他事情突然出现在我身上。在错误中,它说return new N(d1, d2); 。 他们使用了extends一词。出于绝望,我试图添加以下类型。 DoublyNestedGenerics.java:50: error: incompatible types: N cannot be converted to D return new N(d1, d2); ^ where D is a type-variable: D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer) 1 error 然后,从那里,我将回报更改为这么说。 where D is a type-variable: D extends M<Integer,Integer>,F<Integer,Integer>然后我按编译。 interface C<C1, C2> extends M<C1, C2>, F<C1, C2> {} record N2 (Integer n1, Integer n2) implements C<Integer, Integer> {} 在这一点上,我很生气。 错误消息以某种方式告诉我final C<Integer, Integer> blah = new N2(d1, d2); return blah; 。这也告诉我,特别是DoublyNestedGenerics.java:55: error: incompatible types: C<Integer,Integer> cannot be converted to D return blah; ^ where D is a type-variable: D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer) 1 error C<Integer,Integer> cannot be converted to DD is a type-variableD extends M<Integer,Integer>,F<Integer,Integer>是C但是,它仍然无法使用吗? 它把我带到这里。我想念什么?再说一次,如果有人愿意提出建议,请一些帮助制作更好的头衔。 最终,这是完整的代码,以防万一很难遵循。 public class DoublyNestedGenerics { interface M<K, V> {} interface F<I, O> {} record A <A1, A2> (A1 a1, A2 a2) implements M<A1, A2>, F<A1, A2> {} record N (Integer n1, Integer n2) implements M<Integer, Integer>, F<Integer, Integer> {} interface C<C1, C2> extends M<C1, C2>, F<C1, C2> {} record N2 (Integer n1, Integer n2) implements C<Integer, Integer> {} private static < D1, D2, D extends M<D1, D2> & F<D1, D2> > D canItWork(final D1 d1, final D2 d2) { // return new A<>(d1, d2); throw new UnsupportedOperationException(); } private static < D extends M<Integer, Integer> & F<Integer, Integer> > D canItWorkAttempt2(final Integer d1, final Integer d2) { final C<Integer, Integer> blah = new N2(d1, d2); return blah; // return new N(d1, d2); // throw new UnsupportedOperationException(); } } 您缺少的是,D不是通过canItWork方法来参数化,而是该方法的caller。您是对的,A符合D的界限,但任何其他类型也是如此。 canItWork方法不知道呼叫者期望哪种类型。 考虑以下内容: public class GenericTesting { interface Foo<T, U> {} interface Bar<T, U> {} record FooBar<T, U>(T t, U u) implements Foo<T, U>, Bar <T, U> {} record OtherFooBar<T, U>(T t, U u) implements Foo<T, U>, Bar<T, U> {} static <T, U, R extends Foo<T, U> & Bar<T, U>> R test(T t, U u) { // ERROR - Type mismatch: cannot convert from GenericTesting.FooBar<T,U> to R return new FooBar<T, U>(t, u); } public static void main(String[] args) { // This compiles without problem. Which is why the 'test' method // *does not* compile, because then you'd be trying to assign a // FooBar instance to an OtherFooBar variable. OtherFooBar<String, String> ofb = test("Hello", "World!"); } }

回答 0 投票 0





Julia 如何知道某个对象的类型是什么?对 `Vector{Int64}` 的引用的内存布局是什么?

我试图了解有关 Julia 类型系统如何工作的更多细节。 考虑下面的简单例子 朱莉娅> v = [1] 1 元素向量{Int64}: 1 朱莉娅> typeof(v) Vector{Int64}(等...

回答 1 投票 0

从对象字面量类型中另一个函数的返回类型推断函数的参数类型

使用像这样的文字对象类型: 类型 A = { f1: () => R f2: (x: R) => 无效 } 当我声明

回答 1 投票 0

自定义函数中Zod对象键的类型推断

我正在创建一个名为 createDefinition 的函数,它以 Zod 对象作为参数,使用 ZodType 来缩小可以传递给它的允许的 Zod 形状(在本例中,仅使用 JSON)。 然而,要...

回答 1 投票 0

Rust 类型推断因 const generic 失败

我在 Rust 的类型推断中遇到了一个失败,我无法理解 - 正如大多数情况一样,我不知道这是否是编译器错误/不足,或者类型应该...... .

回答 1 投票 0

尽管调用函数时 T 和 U 相同,但运算符 '+' 不能应用于类型 'T' 和 'U'

我知道这个问题已经在 StackOverflow 和 GitHub 上被问过。在这个问题上,我想延长他们的谈话。正如 Typescript 核心团队成员 RyanCavanaugh 所说: 一切

回答 1 投票 0

如何修复打字稿处理函数重载和类型推断

我有这个游乐场,我想让它在我的情况下发挥作用。 类型人 = { 名称:字符串; 姓氏:字符串; } 类型用户=人&{ 用户名:字符串; } 类型名称=字符串; 功能

回答 1 投票 0

Go 泛型与混合无类型参数的类型不匹配

func sum[T int | ] float64](a, b T) T { fmt.Println("a 的类型:",reflect.TypeOf(a)) fmt.Println("b 的类型:",reflect.TypeOf(b)) 返回 a + b } 输入 Pair[K, V 任意]

回答 2 投票 0

Rust 的类型推断如何跨多个语句工作?

Rust 在相当高级的情况下执行类型推断。有人可以解释(或指出)描述可以推断什么和不能推断什么的规则吗? 第一个很简单:a 的类型

回答 1 投票 0

如何通过JSDoc正确推断对象的构造函数?

我想让这个函数接收一个参数并返回它的类(如果是对象)、参数本身(如果是函数)或遗留对象构造函数(如果有其他)。 在 JavaScript 中的示例...

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.