Rust: 检测何时可以将“不受约束的类型参数”显式提供给 fn 调用

创建于 2017-02-21  ·  3评论  ·  资料来源: rust-lang/rust

https://github.com/rust-lang/rust/pull/39361的基础上,我们可能需要考虑用户何时应该向 fn 调用添加显式类型参数。 我正在想象一个这样的例子:

fn foo<T>() { }

fn main() {
    foo();
}

建议用户为一些明确的T编写foo::<T>会很好。 这需要一些思考:

我们什么时候想建议这个而不是注释局部变量? 我更愿意在局部变量上附加注释,但如果局部变量的类型是一些复杂的东西,它在其中深深地提到了未推断的类型,则可能不会,而注释函数将允许我们直接指定未推断的类型.

一个例子:

fn foo<T>() -> Option<T> { }

fn main() {
    let x = foo();
}

我们应该建议标记x: Option<T>还是foo::<T> ? 这种情况是临界的,但如果你用一些更复杂的类型替换Option ,它会进一步倾斜平衡。

当 fn 有多种类型,而我们只知道它们的一部分时,我们该怎么办? 当我们有一些信息但不是全部时,我们回避了之前应该做什么的问题。 但感觉这里可能更重要。 一个例子:

fn foo<T, U>() -> T { }

fn main() {
    let x: i32 = foo();
}

在这里,我们知道T ,但不知道U 。 我们应该建议foo::<_, XXX>吗? 我们如何使用XXX来向用户表明这是他们需要提供的东西?

cc @cengizIO -- 有兴趣追求吗?

cc @estebank @jonathandturner——关于如何表达的想法?

A-diagnostics C-enhancement E-needs-mentor T-compiler WG-compiler-errors

最有用的评论

电流输出:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required

一些选项:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required
   = note: generic parameter `U` needs to be specified for foo::<T, U>()
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |                  |
   |                  you can specify the generic parameter here using `foo::<_, U>()`
   |
   = note: generic parameter `U` needs to be specified for `fn foo::<T, U>() -> T`
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U` in `fn foo<T, U>() -> T`
   |                  |
   |                  specify `U` using `foo::<_, U>()`
   |

所有3条评论

你好@nikomatsakis

我会在这方面工作。

再次感谢!

电流输出:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required

一些选项:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required
   = note: generic parameter `U` needs to be specified for foo::<T, U>()
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |                  |
   |                  you can specify the generic parameter here using `foo::<_, U>()`
   |
   = note: generic parameter `U` needs to be specified for `fn foo::<T, U>() -> T`
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U` in `fn foo<T, U>() -> T`
   |                  |
   |                  specify `U` using `foo::<_, U>()`
   |

这张纸条尤其让我眼前一亮:

   = note: type annotations or generic parameter binding required

让我们绝对删除那个。 :)

此页面是否有帮助?
0 / 5 - 0 等级