Rust: اكتشاف متى يمكن توفير "معلمات النوع غير المقيدة" بشكل صريح لاستدعاء fn

تم إنشاؤها على ٢١ فبراير ٢٠١٧  ·  3تعليقات  ·  مصدر: rust-lang/rust

بناءً على https://github.com/rust-lang/rust/pull/39361 ، قد نرغب في التفكير في الوقت الذي يجب فيه على المستخدمين إضافة معلمات نوع صريحة إلى مكالمة fn. أنا أتخيل مثالًا مثل هذا:

fn foo<T>() { }

fn main() {
    foo();
}

سيكون من الجيد اقتراح أن يكتب المستخدم foo::<T> لبعض 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 للإشارة إلى المستخدم أن هذا هو الشيء الذي يحتاج إلى تقديمه؟

cccengizIO - ممتع في المتابعة؟

ccestebankjonathandturner - أفكار حول كيفية العبارة؟

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 التقييمات