Rust: detectar cuándo se pueden proporcionar explícitamente "parámetros de tipo sin restricciones" a una llamada fn

Creado en 21 feb. 2017  ·  3Comentarios  ·  Fuente: rust-lang/rust

Sobre la base de https://github.com/rust-lang/rust/pull/39361 , es posible que deseemos considerar cuándo los usuarios deben agregar parámetros de tipo explícitos a una llamada fn. Me estoy imaginando un ejemplo como este:

fn foo<T>() { }

fn main() {
    foo();
}

Sería bueno sugerir que el usuario escriba foo::<T> para algunos T explícitos. Esto requerirá un poco de reflexión:

¿Cuándo queremos sugerir esto en lugar de anotar una variable local? Preferiría adjuntar la anotación en la variable local, pero tal vez no si el tipo de la variable local es algo complejo que menciona el tipo no inferido profundamente dentro de ella, mientras que anotar la función nos permitiría especificar el tipo no inferido directamente .

Un ejemplo:

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

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

¿Deberíamos sugerir etiquetar x: Option<T> o foo::<T> ? Este caso está en el límite, pero si reemplaza Option con algún tipo más complejo, inclina aún más la balanza.

¿Qué hacemos cuando el fn toma muchos tipos y los conocemos parcialmente? Eludimos antes el problema de qué hacer cuando tenemos alguna información pero no toda. Pero parece que aquí puede ser más importante. Un ejemplo:

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

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

Aquí conocemos T , pero no U . ¿Deberíamos sugerir foo::<_, XXX> ? ¿Cómo expresamos los XXX para indicarle al usuario que esto es lo que necesita proporcionar?

cc @cengizIO -- ¿interesado en seguir?

cc @estebank @jonathandturner -- ¿pensamientos sobre cómo expresar?

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

Comentario más útil

Salida de corriente:

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

Algunas opciones:

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>()`
   |

Todos 3 comentarios

Hola @nikomatsakis

Estaré trabajando en esto.

¡Gracias de nuevo!

Salida de corriente:

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

Algunas opciones:

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>()`
   |

Esta nota en particular hace que mis ojos se pongan vidriosos:

   = note: type annotations or generic parameter binding required

Definitivamente eliminemos ese. :)

¿Fue útil esta página
0 / 5 - 0 calificaciones