القيود (F#)

يصف هذا pic إلى القيود التي يمكن تطبيقها إلى معلمات نوع عامة إلى تحديد متطلبات وسيطة نوع في نوع عام أو دالة.

type-parameter-list when constraint1 [ and constraint2]

ملاحظات

هناك عدة قيود مختلفة يمكنك يطبق إلى تحدد أنواع التي يمكن استخدامها في نوع عام. The following جدول lists و describes these constraints.

قيد

بناء الجملة

الوصف

نوع قيد

type-parameter :> type

The provided نوع must be يساوي أو derived من the نوع specified, أو, if the نوع هو an واجهة, the provided نوع must implement the واجهة.

Null قيد

type-parameter : null

The provided نوع must دعم the null قيمة حرفية. This يتضمن الجميع .NET كائن أنواع but not F# قائمة, tuple, دالة, فئة, سجل, أو توحيد أنواع.

Explicit عضو قيد

type-parameter[أو ... أوtype-parameter] : member-signature

على الأقل واحد of the نوع الوسيطات provided must have a عضو that has the specified توقيع; not intended for عام استخدم.

الدالة الإنشائية قيد

type-parameter: ( جديد : unit -> 'a )

The provided نوع must have a الافتراضي الدالة الإنشائية.

نوع القيمة القيد

: بنية

The provided نوع must be a .NET القيمة نوع.

نوع المرجع القيد

: not بنية

The provided نوع must be a .NET مرجع نوع.

قائمة تعداد نوع قيد

: enum<underlying-type>

The provided نوع must be an enumerated نوع that has the specified underlying نوع; not intended for عام استخدم.

تفويض قيد

: تفويض<tuple-parameter-type,return-type>

The provided نوع must be a تفويض نوع that has the specified الوسيطات و return القيمة; not intended for عام استخدم.

المقارنة قيد

: المقارنة

The provided نوع must دعم المقارنة.

Equality قيد

: equality

The provided نوع must دعم equality.

Unmanaged قيد

: unmanaged

The provided نوع must be an unmanaged نوع. Unmanaged أنواع are either certain primitive أنواع (sbyte, byte, char, nativeint, unativeint, float32, float, int16, uint16, int32, uint32, int64, uint64, أو decimal), قائمة تعداد أنواع, nativeptr<_>, أو a non-generic بنية whose حقول are الجميع unmanaged أنواع.

You have إلى إضافة a قيد when your تعليمات برمجية has إلى استخدم a ميزة that هو متوفر تشغيل the قيد نوع but not تشغيل أنواع في عام. For مثال, if you استخدم the نوع قيد إلى specify a فئة نوع, you can استخدم أي واحد of the وظائف of that فئة في the generic دالة أو نوع.

Specifying constraints هو sometimes مطلوب when writing نوع معلمات بوضوح, because without a قيد, the compiler has لا way of verifying that the features that you are using will be متوفر تشغيل أي نوع that might be supplied at run الوقت for the نوع معلمة.

The most عام constraints you استخدم في F# تعليمات برمجية are نوع constraints that specify الأساس فئات أو الواجهات. The غير ذلك constraints are either used بواسطة the F# مكتبة إلى implement certain functionality, such كـ the explicit عضو قيد, which هو used إلى implement التحميل الزائد لعامل التشغيل for arithmetic عوامل تشغيل, أو are provided mainly because F# supports the إكمال التعيين of constraints that هو supported بواسطة the وقت تشغيل اللغة العامة.

During the نوع inference عملية, some constraints are inferred automatically بواسطة the compiler. For مثال, if you استخدم the + عامل في a دالة, the compiler infers an explicit عضو قيد تشغيل متغير أنواع that are used في the تعبير.

راجع أيضًا:

المرجع

عام (F#)

القيود (F#)