قيم (F#)

تكون القيم في F# الكميات التي تحتوي على نوع معين؛ يمكن أن تكون القيم المتكاملة أو فاصلة عائمة أرقام أحرف أو النص، القوائم، تسلسل، صفائف، المجموعات عن، اتحادات discrimفيated، السجلات، فئة أو أنواع قيم الدالة.

ربط a القيمة

The term ربط means associating a اسم مع a تعريف. The let كلمة أساسية binds a القيمة, كـ في the following أمثلة:

let a = 1
let b = 100u
let str = "text"

// A function value binding.

let f x = x + 1

The نوع of a القيمة هو inferred من the تعريف. For a primitive نوع, such كـ an integral أو عائم يؤشر رقم, the نوع هو determined من the نوع of the قيمة حرفية. Therefore, في the السابق مثال, the compiler infers the نوع of b إلى be unsigned int, whereas the compiler infers the نوع of a إلى be int. The نوع of a دالة القيمة هو determined من the return القيمة في the دالة النص الأساسي. ل المزيد من المعلومات حول أنواع القيمة دالة، راجع الدالات (F#). For المزيد معلومات حول قيمة حرفية أنواع, see القيم الحرفية (F#).

Why Immutable?

Immutable قيم are قيم that cannot be تم تغييره throughout the course of a برنامج's execution. If you are used إلى languages such كـ C++, Visual أساسى, أو C#, you might بحث it surprising that F# puts primacy over immutable قيم rather than متغيرات that can be assigned جديد قيم during the execution of a برنامج. Immutable بيانات هو an important عنصر of functional programming. في a multithreaded بيئة, shared mutable متغيرات that can be تم تغييره بواسطة many different عمليات جزئية are difficult إلى إدارة. Also, مع mutable متغيرات, it can sometimes be hard إلى tell if a متغير might be تم تغييره when it هو passed إلى another دالة.

في pure functional languages, there are لا متغيرات, و دالات behave strictly كـ رياضى دالات. الموقع تعليمات برمجية في a procedural اللغة uses a متغير assignment إلى alter a القيمة, the equivalent تعليمات برمجية في a functional اللغة has an immutable القيمة that هو the إدخال, an immutable دالة, و different immutable قيم كـ the إخراج. This رياضى strictness allows for tighter reasoning حول the سلوك of the برنامج. This tighter reasoning هو what enables compilers إلى فحص تعليمات برمجية المزيد stringently و إلى أمثلية المزيد effectively, و helps make it easier for developers إلى understand و write correct تعليمات برمجية. Functional تعليمات برمجية هو therefore likely إلى be easier إلى يصحح than ordinary procedural تعليمات برمجية.

F# هو غير لغة فعالة فقط، غير أنه يدعم بشكل كامل البرمجة الوظيفية. Using immutable قيم هو a بضاعة practice because doing this allows your تعليمات برمجية إلى benefit من an important aspect of functional programming.

Mutable متغيرات

You can استخدم the كلمة أساسية mutable إلى specify a متغير that can be تم تغييره. Mutable متغيرات في F# should generally have a limited نطاق, either كـ a حقل of a نوع أو كـ a محلي القيمة. Mutable متغيرات مع a limited نطاق are easier إلى عنصر تحكم و are أقل likely إلى be ‏‏تاريخ التعديل في incorrect ways.

You can تعيين an القيمة الإفتراضية إلى a mutable متغير بواسطة using the let كلمة أساسية في the same way كـ you would define a value. However, the difference هو that you can subsequently تعيين جديد قيم إلى mutable متغيرات بواسطة using the <- عامل, كـ في the following مثال.

let mutable x = 1
x <- x + 1

مواضيع ذات صلة

Title

الوصف

ترك للربط (F#)

Provides معلومات حول using the let كلمة أساسية إلى يربط أسماء إلى قيم و دالات.

الدالات (F#)

Provides an overview of دالات في F#‎.

راجع أيضًا:

المبادئ

قيم خالية (F#)

موارد أخرى

مرجع لغة ب #