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#.

TitleTitle 描述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.

TitleTitle 描述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 描述关键字 useusing,它们可以控制资源的初始化和发布Describes 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. 与其他 .NET 语言一样,可采用 F# 从代码注释生成文档。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.

TitleTitle 描述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 类型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. string 是 .NET Framework 中 System.String 的别名。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.

TitleTitle 描述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 表达式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 提供一种方便语法,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