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 評価