السجلات (F#)

تمثل سجلات تجميعات بسيط من قيم المسماة، بشكل اختياري مع الأعضاء.

[ attributes ]
type [accessibility-modifier] typename = { 
    [ mutable ] label1 : type1;
    [ mutable ] label2 : type2;
    ...
    }
    member-list

ملاحظات

في the السابق بناء الجملة, typename هو the اسم of the سجل نوع, label1 و label2 are أسماء of قيم, referred إلى كـ ‏‏التسميات, و type1 و type2 are the أنواع of these قيم. member-list هو the اختياري قائمة of الأعضاء for the نوع.

Following are some أمثلة.

type Point = { x : float; y: float; z: float; }
type Customer = { First : string; Last: string; SSN: uint32; AccountNumber : uint32; }

When each التسمية هو تشغيل a separate خط, the semicolon هو اختياري.

You can التعيين قيم في expressions known كـ سجل expressions. The compiler infers the نوع من the ‏‏التسميات used (if the ‏‏التسميات are sufficiently distinct من those of غير ذلك سجل أنواع). Braces ({ }) enclose the سجل تعبير. The following تعليمات برمجية shows a سجل تعبير that initializes a سجل مع three حُر عناصر مع ‏‏التسميات x, y و z..

let mypoint = { x = 1.0; y = 1.0; z = -1.0; }

Do not استخدم the shortened نموذج if there could be another نوع that also has the same ‏‏التسميات.

type Point = { x : float; y: float; z: float; }
type Point3D = { x: float; y: float; z: float }
// Ambiguity: Point or Point3D?
let mypoint3D = { x = 1.0; y = 1.0; z = 0.0; }

The ‏‏التسميات of the most recently النوع المعلن عنه take precedence over those of the previously النوع المعلن عنه, so في the preceding مثال, mypoint3D هو inferred إلى be Point3D. You can بوضوح specify the سجل نوع, كـ في the following تعليمات برمجية.

let myPoint1 = { Point.x = 1.0; y = 1.0; z = 0.0; }

وظائف can be defined for سجل أنواع just كـ for فئة أنواع.

Creating Records بواسطة Using سجل Expressions

You can يهيّئ records بواسطة using the ‏‏التسميات that are defined في the سجل. An تعبير that does this هو referred إلى كـ a سجل تعبير. استخدم braces إلى enclose the سجل تعبير و استخدم the semicolon كـ a المحدد.

The following مثال shows how إلى إنشاء a سجل.

type MyRecord = {
    X: int;
    Y: int;
    Z: int 
    }

let myRecord1 = { X = 1; Y = 2; Z = 3; }

The semicolons بعد the أخير حقل في the سجل تعبير و في the نوع تعريف are اختياري, regardless of whether the حقول are الجميع في واحد خط.

When you إنشاء a سجل, you must supply قيم for each حقل. You cannot refer إلى the قيم of غير ذلك حقول في the تهيئة تعبير for أي حقل.

في the following تعليمات برمجية, the نوع of myRecord2 هو inferred من the أسماء of the حقول. Optionally, you can specify the نوع اسم بوضوح.

let myRecord2 = { MyRecord.X = 1; MyRecord.Y = 2; MyRecord.Z = 3 }

Another نموذج of سجل construction can be useful when you have إلى نسخ an موجود سجل, و possibly تغيير some of the حقل قيم. The following خط of تعليمات برمجية illustrates this.

let myRecord3 = { myRecord2 with Y = 100; Z = 2 }

This نموذج of the سجل تعبير هو called the نسخ و تحديث سجل تعبير.

Records are immutable بواسطة الافتراضي; however, you can بسهولة إنشاء ‏‏تاريخ التعديل records بواسطة using a نسخ و تحديث تعبير. You can also بوضوح specify a mutable حقل.

type Car = {
    Make : string
    Model : string
    mutable Odometer : int
    }
let myCar = { Make = "Fabrikam"; Model = "Coupe"; Odometer = 108112 }
myCar.Odometer <- myCar.Odometer + 21

نقش Matching مع Records

Records can be used مع نقش matching. You can specify some حقول بوضوح و provide متغيرات for غير ذلك حقول that will be assigned when a مطابقة occurs. The following تعليمات برمجية مثال illustrates this.

let evaluatePoint (point: Point3D) =
    match point with
    | { x = 0.0; y = 0.0; z = 0.0 } -> printfn "Point is at the origin."
    | { x = xVal; y = 0.0; z = 0.0 } -> printfn "Point is on the x-axis. Value is %f." xVal
    | { x = 0.0; y = yVal; z = 0.0 } -> printfn "Point is on the y-axis. Value is %f." yVal
    | { x = 0.0; y = 0.0; z = zVal } -> printfn "Point is on the z-axis. Value is %f." zVal
    | { x = xVal; y = yVal; z = zVal } -> printfn "Point is at (%f, %f, %f)." xVal yVal zVal

evaluatePoint { x = 0.0; y = 0.0; z = 0.0 }
evaluatePoint { x = 100.0; y = 0.0; z = 0.0 }
evaluatePoint { x = 10.0; y = 0.0; z = -1.0 }

The إخراج of this تعليمات برمجية هو كـ follows.

Point is at the origin.
Point is on the x-axis. Value is 100.000000.
Point is at (10.000000, 0.000000, -1.000000).

Differences Between Records و فئات

سجل حقول differ من فئات في that they are automatically exposed كـ خصائص, و they are used في the creation و copying of records. سجل construction also differs من فئة construction. في a سجل نوع, you cannot define a الدالة الإنشائية. Instead, the construction بناء الجملة described في this موضوع applies. فئات have لا direct علاقة between الدالة الإنشائية معلمات, حقول, و خصائص.

مثل توحيد و بنية أنواع, records have structural equality semantics. فئات have مرجع equality semantics. يوضح مثال التعليمة البرمجية التالي هذا.

let record1 = { X = 1; Y = 2 }
let record2 = { X = 1; Y = 2 }
if (record1 = record2) then
    "The records are equal."
else
    "The records are unequal."

If you write the same تعليمات برمجية مع فئات, the الثاني فئة الكائنات would be unequal because the الثاني قيم would represent الثاني الكائنات تشغيل the كومة ذاكرة مؤقتة و فقط the addresses would be compared (unless the فئة نوع overrides the System.Object.Equals أسلوب).

راجع أيضًا:

المرجع

الفئات (F#)

الأنماط (F#)

موارد أخرى

أنواع ب #

مرجع لغة ب #