F# 語言參考F# Language Reference

本章節會參考F#的語言、 以.NET 為目標的多範型程式設計的語言。This section is a reference to the F# language, a multi-paradigm programming language targeting .NET. F# 語言支援函式、物件導向和命令式程式設計語言。The F# language supports functional, object-oriented and imperative programming models.

F# 語彙基元F# Tokens

下表顯示的參考主題提供作為 F# 語彙基元之關鍵字、符號和常值的表格。The following table shows reference topics that provide tables of keywords, symbols and literals used as tokens in F#.

標題Title 描述Description
關鍵字參考Keyword Reference 包含所有 F# 語言關鍵字相關資訊的連結。Contains links to information about all F# language keywords.
符號和運算子參考Symbol and Operator Reference 包含 F# 語言使用之符號和運算子的表格。Contains a table of symbols and operators that are used in the F# language.
常值Literals 描述 F# 常值的語法以及如何指定 F# 常值的類型資訊。Describes the syntax for literal values in F# and how to specify type information for F# literals.

F# 語言概念F# Language Concepts

下表顯示描述語言概念的可用參考主題。The following table shows reference topics available that describe language concepts.

標題Title 描述Description
函式Functions 函式是所有程式設計語言的基礎程式執行單位。Functions are the fundamental unit of program execution in any programming language. 如同其他語言,F# 函式有名稱、可以有參數並且接受引數,而且也有主體。As in other languages, an F# function has a name, can have parameters and take arguments, and has a body. F# 也支援函式程式設計建構,例如將函式視為值、在運算式中使用不具名函式、組合函式以形成新函式、局部調用函式,以及透過部分套用函式引數的隱含定義函式。F# also supports functional programming constructs such as treating functions as values, using unnamed functions in expressions, composition of functions to form new functions, curried functions, and the implicit definition of functions by way of the partial application of function arguments.
F# 類型F# Types 描述 F# 中所使用的類型以及 F# 類型的命名及描述方式。Describes the types that are used in F# and how F# types are named and described.
類型推斷Type Inference 描述 F# 編譯器如何推斷值、變數、參數和傳回值的類型。Describes how the F# compiler infers the types of values, variables, parameters and return values.
自動一般化Automatic Generalization 描述 F# 的泛型建構。Describes generic constructs in F#.
繼承Inheritance 描述繼承,這是用來在物件導向程式設計中建立 "is-a" 關聯性 (或稱子類型) 的模型。Describes inheritance, which is used to model the "is-a" relationship, or subtyping, in object-oriented programming.
成員Members 描述 F# 物件類型的成員。Describes members of F# object types.
參數和引數Parameters and Arguments 描述對定義參數以及將引數傳遞至函式、方法和屬性的語言支援,Describes language support for defining parameters and passing arguments to functions, methods, and properties. 其中包含如何以參考方式傳遞的詳細資訊。It includes information about how to pass by reference.
運算子多載Operator Overloading 描述如何在類別或記錄類型以及共用層多載算術運算子。Describes how to overload arithmetic operators in a class or record type, and at the global level.
轉型和轉換Casting and Conversions 描述 F# 中對類型轉換的支援。Describes support for type conversions in F#.
存取控制Access Control 描述 F# 的存取控制。Describes access control in F#. 存取控制表示宣告哪些用戶端能夠使用特定程式項目,例如類型、方法和函式等等。Access control means declaring what clients are able to use certain program elements, such as types, methods, functions and so on.
模式比對Pattern Matching 描述模式,這是轉換輸入資料的規則,在 F# 語言中用於擷取具有模式的比較資料、將資料分解為構成部分,或是以各種方式從資料擷取資訊。Describes patterns, which are rules for transforming input data that are used throughout the F# language to extract compare data with a pattern, decompose data into constituent parts, or extract information from data in various ways.
使用中模式Active Patterns 描述作用中的模式。Describes active patterns. 作用中的模式可讓您定義可細分輸入資料的具名部分。Active patterns enable you to define named partitions that subdivide input data. 您可以使用作用中的模式,以自訂方式分解每個部分的資料。You can use active patterns to decompose data in a customized manner for each partition.
判斷提示Assertions 描述 assert 運算式,這個偵錯功能可用來測試運算式。Describes the assert expression, which is a debugging feature that you can use to test an expression. 當運算式在偵測模式中發生錯誤時,判斷提示會顯示系統錯誤對話方塊。Upon failure in Debug mode, an assertion generates a system error dialog box.
例外狀況處理Exception Handling 包含 F# 語言例外狀況處理支援的資訊。Contains information about exception handling support in the F# language.
屬性attributes 描述可讓中繼資料套用至程式設計建構的屬性。Describes attributes, which enable metadata to be applied to a programming construct.
資源管理:use 關鍵字Resource Management: The use Keyword 描述可控制資源初始設定及解除的關鍵字 useusingDescribes the keywords use and using, which can control the initialization and release of resources
命名空間namespaces 描述 F# 的命名空間支援。Describes namespace support in F#. 命名空間可讓您將名稱附加至程式項目群組,將程式碼依相關功能分類。A namespace lets you organize code into areas of related functionality by enabling you to attach a name to a grouping of program elements.
模組Modules 描述模組。Describes modules. F# 模組是 F# 程式碼群組,例如 F# 程式中的值、類型和函式值。An F# module is a grouping of F# code, such as values, types, and function values, in an F# program. 程式碼分組成不同模組有助於將相關程式碼整理到同一處,以及避免程式中發生名稱衝突。Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.
匯入宣告:open 關鍵字Import Declarations: The open Keyword 描述 open 的運作方式。Describes how open works. 使用匯入宣告指定某個模組或命名空間後,就可以直接參考該模組或命名空間內的項目,而無須使用完整名稱。An import declaration specifies a module or namespace whose elements you can reference without using a fully qualified name.
簽章Signatures 描述簽章和簽章檔。Describes signatures and signature files. 簽章檔案包含一組 F# 程式項目的公開金鑰相關資訊,例如類型、命名空間和模組。A signature file contains information about the public signatures of a set of F# program elements, such as types, namespaces, and modules. 它可用來指定這些程式項目的協助工具。It can be used to specify the accessibility of these program elements.
XML 文件XML Documentation 描述針對 XML 文件註解 (也稱為三斜線註解) 產生文件檔案的支援。Describes support for generating documentation files for XML doc comments, also known as triple slash comments. 您可以從 F# 的程式碼註解產生文件,就如同其他 .NET 語言一樣。You can produce documentation from code comments in F# just as in other .NET languages.
詳細語法Verbose Syntax 描述未啟用輕量型語法時的 F# 建構語法。Describes the syntax for F# constructs when lightweight syntax is not enabled. 詳細語法是透過程式碼頂端的 #light "off" 指示詞所表示。Verbose syntax is indicated by the #light "off" directive at the top of the code file.

F# 類型F# Types

下表顯示描述 F# 語言所支援類型的可用參考主題。The following table shows reference topics available that describe types supported by the F# language.

標題Title 描述Description
values 描述值,這是具有特定類型且不可變的數量;值可以是整數或浮點數、字元或文字、清單、序列、陣列、元組、差別聯集、記錄、類別類型或函式值。Describes values, which are immutable quantities that have a specific type; values can be integral or floating point numbers, characters or text, lists, sequences, arrays, tuples, discriminated unions, records, class types, or function values.
基本類型Basic Types 描述基本的基本類型中所使用的F#語言。Describes the fundamental basic types that are used in the F# language. 它也會提供對應的 .NET 類型以及每個類型的最小值和最大值。It also provides the corresponding .NET types and the minimum and maximum values for each type.
單位類型Unit Type 描述 unit 類型,這個類型表示缺少特定值;unit 類型只有單一值,作為沒有或不需要其他值時的預留位置。Describes the unit type, which is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed.
字串Strings 描述 F# 中的字串。Describes strings in F#. string 類型以一連串的 Unicode 字元表示不可變文字。The string type represents immutable text, as a sequence of Unicode characters. stringSystem.String 在 .NET Framework 中的別名。string is an alias for System.String in the .NET Framework.
元組Tuples 描述元組,這是不具名但有序之值 (可能是不同的類型) 的群組。Describes tuples, which are groupings of unnamed but ordered values of possibly different types.
F# 集合類型F# Collection Types F# 函式集合類型的概觀,包括陣列、清單、序列 (seq)、對應和集的類型。An overview of the F# functional collection types, including types for arrays, lists, sequences (seq), maps, and sets.
清單Lists 描述清單。Describes lists. F# 的清單是一連串有序且不可變的項目,而所有項目的類型都相同。A list in F# is an ordered, immutable series of elements all of the same type.
選項Options 描述選項類型。Describes the option type. F# 中的選項於不一定有值時使用。An option in F# is used when a value may or may not exist. 選項有基礎類型,而且可能擁有該類型的值或沒有值。An option has an underlying type and may either hold a value of that type or it may not have a value.
序列Sequences 描述序列。Describes sequences. 序列是一連串的邏輯項目,而所有項目的類型都相同。A sequence is a logical series of elements all of one type. 個別序列項目只在需要時才會計算,因此表示法可能會比常值項目計數還小。Individual sequence elements are only computed if required, so the representation may be smaller than a literal element count indicates.
陣列Arrays 描述陣列。Describes arrays. 陣列是一系列固定大小、以零起始、可變的連續資料項目,而所有項目的類型都相同。Arrays are fixed-size, zero-based, mutable sequences of consecutive data elements, all of the same type.
記錄Records 描述記錄。Describes records. 記錄表示具名值的簡單彙總,並選擇性地搭配成員。Records represent simple aggregates of named values, optionally with members.
差別聯集Discriminated Unions 描述差別聯集,此種類型支援的值可以是各式各樣具名案例的其中任何一種,且每個聯集的值和類型也不一定相同。Describes discriminated unions, which provides support for values which may be one of a variety of named cases, each with possibly different values and types.
列舉Enumerations 描述列舉,這是具有一組已定義之具名值的類型。Describes enumerations are types that have a defined set of named values. 列舉可用來取代常值,讓程式碼更容易閱讀及維護。You can use them in place of literals to make code more readable and maintainable.
參考儲存格Reference Cells 描述參考儲存格,這是可讓您以參考語意建立可變變數的儲存位置。Describes reference cells, which are storage locations that enable you to create mutable variables with reference semantics.
類型縮寫Type Abbreviations 描述類型縮寫,這是類型的替代名稱。Describes type abbreviations, which are alternate names for types.
類別Classes 描述類別,這是表示可以有屬性、方法和事件之物件的類型。Describes classes, which are types that represent objects that can have properties, methods, and events.
結構Structures 描述結構,這是一種比較精簡的物件類型,適用於資料量較少且行為簡單的類型,效率比類別更好。Describes structures, which are compact object types that can be more efficient than a class for types that have a small amount of data and simple behavior.
介面Interfaces 描述介面,介面可指定其他類別應提供實作的一組相關成員。Describes interfaces, which specify sets of related members that other classes implement.
抽象類別Abstract Classes 描述抽象類別,這種類別不見得會實作出所有或部分成員,而是留待衍生類別各自提供實作。Describes abstract classes, which are classes that leave some or all members unimplemented, so that implementations can be provided by derived classes.
類型延伸模組Type Extensions 描述類型擴充,可讓您將成員新增至先前定義的物件類型。Describes type extensions, which let you add new members to a previously defined object type.
彈性類型Flexible Types 描述彈性類型。Describes flexible types. 彈性類型註解表示參數、變數或值的類型與指定的類型相容,而相容性是由類別或介面的物件導向階層中的位置所決定。A flexible type annotation is an indication that a parameter, variable or value has a type that is compatible with type specified, where compatibility is determined by position in an object-oriented hierarchy of classes or interfaces.
委派Delegates 描述將函式呼叫表示為物件的委派。Describes delegates, which represent a function call as an object.
測量單位Units of Measure 描述測量單位。Describes units of measure. F# 中的浮點值可以有通常用來表示長度、容量、質量等的關聯測量單位。Floating point values in F# can have associated units of measure, which are typically used to indicate length, volume, mass, and so on.
類型提供者Type Providers 描述型別提供者,並且提供逐步解說如何使用內建的型別提供者來存取資料庫和 Web 服務的連結。Describes type provides and provides links to walkthroughs on using the built-in type providers to access databases and web services.

F# 運算式F# Expressions

下表列出描述 F# 運算式的主題。The following table lists topics that describe F# expressions.

標題Title 描述Description
條件運算式:if...then...elseConditional Expressions: if...then...else 描述 if...then...else 運算式,這個運算式會根據指定的布林運算式,執行不同的程式碼分支,也會運算出不同的值。Describes the if...then...else expression, which runs different branches of code and also evaluates to a different value depending on the Boolean expression given.
比對運算式Match Expressions 描述 match 運算式,此種運算式提供分支控制,可根據運算式與一組模式的比較結果,決定程式應沿著哪個分支繼續執行。Describes the match expression, which provides branching control that is based on the comparison of an expression with a set of patterns.
迴圈:for...to 運算式Loops: for...to Expression 描述 for...to 運算式,這個運算式會重複執行某段迴圈,重複次數等於迴圈變數的範圍值。Describes the for...to expression, which is used to iterate in a loop over a range of values of a loop variable.
迴圈:for...in 運算式Loops: for...in Expression 描述 for...in 運算式,這個迴圈建構會使用可列舉集合 (例如範圍運算式、序列、清單、陣列或其他支援列舉的建構) 中符合模式的所有項目,重複執行一段程式碼。Describes the for...in expression, a looping construct that is used to iterate over the matches of a pattern in an enumerable collection such as a range expression, sequence, list, array, or other construct that supports enumeration.
迴圈:while...do 運算式Loops: while...do Expression 描述 while...do 運算式,當指定的測試條件為 true 時,用來重複執行一次 (迴圈)。Describes the while...do expression, which is used to perform iterative execution (looping) while a specified test condition is true.
物件運算式Object Expressions 描述物件運算式,這些運算式會根據現有的基底類型、一個介面或一組介面來建立動態建立、匿名物件類型的新執行個體。Describes object expressions, which are expressions that create new instances of a dynamically created, anonymous object type that is based on an existing base type, interface, or set of interfaces.
延遲運算式Lazy Expressions 描述延遲運算式,這是不會立即進行評估,但會改為評估,結果實際上在需要時的計算。Describes lazy expressions, which are computations that are not evaluated immediately, but are instead evaluated when the result is actually needed.
計算運算式Computation Expressions 描述 F# 中的計算運算式提供便利的語法,用於撰寫可以使用控制流程建構和繫結進行排序和合併的計算。Describes computation expressions in F#, which provide a convenient syntax for writing computations that can be sequenced and combined using control flow constructs and bindings. 它們可以用於提供 monad 的便利語法,這是一種函式程式設計功能,可用來管理函式程式中的資料、控制項和副作用。They can be used to provide a convenient syntax for monads, a functional programming feature that can be used to manage data, control and side effects in functional programs. 非同步工作流程是一種計算運算式,支援非同步和平行計算。One type of computation expression, the asynchronous workflow, provides support for asynchronous and parallel computations. 如需詳細資訊,請參閱非同步工作流程For more information, see Asynchronous Workflows.
非同步工作流程Asynchronous Workflows 描述非同步工作流程,這是一種語言功能,可讓您使用與原本撰寫同步程式碼極為相似的方式,來撰寫非同步程式碼。Describes asynchronous workflows, a language feature that lets you write asynchronous code in a way that is very close to the way you would naturally write synchronous code.
程式碼引號Code Quotations 描述程式碼引號,此語言功能可讓您以程式設計方式產生及使用 F# 程式碼運算式。Describes code quotations, a language feature that enables you to generate and work with F# code expressions programmatically.
查詢運算式Query Expressions 描述查詢運算式,這種語言功能可為 F# 實作 LINQ,並且可讓您針對資料來源或可列舉集合撰寫查詢。Describes query expressions, a language feature that implements LINQ for F# and enables you to write queries against a data source or enumerable collection.

編譯器支援的建構Compiler-supported Constructs

下表列出描述編譯器支援之特殊建構的主題。The following table lists topics that describe special compiler-supported constructs.

主題Topic 描述Description
編譯器選項Compiler Options 描述 F# 編譯器的命令列選項。Describes the command-line options for the F# compiler.
編譯器指示詞Compiler Directives 描述處理器指示詞和編譯器指示詞。Describes processor directives and compiler directives.
原始碼程式行、檔案與路徑識別項Source Line, File, and Path Identifiers 描述識別項 __LINE____SOURCE_DIRECTORY____SOURCE_FILE__,這些內建值可讓您存取原始程式碼中的行號、目錄和檔案名稱。Describes the identifiers __LINE__, __SOURCE_DIRECTORY__ and __SOURCE_FILE__, which are built-in values that enable you to access the source line number, directory and file name in your code.

另請參閱See also