specyfikacja_Fis.pdf

(5147 KB) Pobierz
The F# 2.0 Language Specification
Note: This documentation is the specification of the 2.0 release of F# made by Microsoft Research and the
Microsoft Developer Division in April 2010.
Discrepancies may exist between this specification and the 2.0 implementation. Some of these are noted as
comments in this document. If you find further discrepancies please contact us and we will gladly address the
issue in future releases of this specification. The F# team is always very grateful for feedback on this
specification, and on both the design and implementation of F#. You can submit feedback by emailing
The latest version of this specification can be found at www.fsharp.net . Many thanks to the F# user community
for their helpful feedback on the document so far.
Certain parts of this specification refer to the C# 4.0, Unicode, and IEEE specifications.
Authors: Don Syme, with assistance from Anar Alimov,Keith Battocchi, Jomo Fisher, Michael Hale, Jack Hu,
Luke Hoban, Tao Liu, Dmitry Lomov, James Margetson, Brian McNamara, Joe Pamer, Penny Orwick, Daniel
Quirk, Chris Smith, Matteo Taveggia and others.
Notice
© 2005-2012 Microsoft Corporation. Made available under the Apache 2.0 License as part of F# 2.0 source code.
Microsoft, Windows, and Visual F# are either registered trademarks or trademarks of Microsoft Corporation in the
U.S.A. and/or other countries/regions.
Other product and company names mentioned herein may be the trademarks of their respective owners.
Document Updates:
Updated with glossary, index, and style corrections, August 2010
Updated with glossary, index, and style corrections, February 2011
Updated with grammar summary, December 2011
Updated with formatting changes, April 2012
Table of Contents
1.
Lightweight Syntax ........................................................................................................................................ 11
1.1.2
Making Data Simple ...................................................................................................................................... 12
1.1.3
Making Types Simple ..................................................................................................................................... 12
1.1.4
Functional Programming ............................................................................................................................... 13
1.1.5
Imperative Programming .............................................................................................................................. 14
1.1.6
.NET Interoperability and CLI Fidelity............................................................................................................. 14
1.1.7
Parallel and Asynchronous Programming ..................................................................................................... 15
1.1.8
Strong Typing for Floating-Point Code........................................................................................................... 15
1.1.9
Object-Oriented Programming and Code Organization................................................................................. 16
1.2
 
2.
3.
3.8.1
Post-filtering of Adjacent Prefix Tokens......................................................................................................... 26
3.8.2
Post-filteringofIntegersFollowedbydjacent“..”....................................................................................... 26
3.8.3
Reserved Numeric Literal Forms .................................................................................................................... 26
3.9
3.10
H IDDEN T OKENS ...................................................................................................................................................... 27
3.11
I DENTIFIER R EPLACEMENTS ......................................................................................................................................... 27
4.
Categorization of Symbolic Operators ........................................................................................................... 32
4.4.2
Precedence of Symbolic Operators and Pattern/Expression Constructs ........................................................ 33
5.
5.4.1
Characteristics of Type Definitions ................................................................................................................ 43
5.4.2
Expanding Abbreviations and Inference Equations ....................................................................................... 44
5.4.3
Type Variables and Definition Sites ............................................................................................................... 45
5.4.4
Base Type of a Type ....................................................................................................................................... 45
2
5.4.5
Interfaces Types of a Type ............................................................................................................................. 46
5.4.6
Type Equivalence ........................................................................................................................................... 46
5.4.7
Subtyping and Coercion ................................................................................................................................. 46
5.4.8
Nullness ......................................................................................................................................................... 47
5.4.9
Default Initialization ...................................................................................................................................... 48
5.4.10
Dynamic Conversion Between Types ............................................................................................................. 48
6.
6.5.1
Parenthesized and Block Expressions ............................................................................................................ 76
6.5.2
Sequential Execution Expressions .................................................................................................................. 77
6.5.3
Conditional Expressions ................................................................................................................................. 77
6.5.4
Shortcut Operator Expressions ...................................................................................................................... 77
6.5.5
Pattern-Matching Expressions and Functions ............................................................................................... 78
6.5.6
Sequence Iteration Expressions ..................................................................................................................... 78
6.5.7
Simple for-Loop Expressions .......................................................................................................................... 79
6.5.8
While Expressions .......................................................................................................................................... 79
6.5.9
Try-with Expressions ...................................................................................................................................... 80
6.5.10
Reraise Expressions........................................................................................................................................ 80
6.5.11
Try-finally Expressions ................................................................................................................................... 80
3
6.9.1
Values and Execution Context ....................................................................................................................... 87
6.9.2
Parallel Execution and Memory Model.......................................................................................................... 88
6.9.3
Zero Values .................................................................................................................................................... 89
6.9.4
Taking the Address of an Elaborated Expression ........................................................................................... 89
6.9.5
Evaluating Value References ......................................................................................................................... 90
6.9.6
Evaluating Function Applications .................................................................................................................. 90
6.9.7
Evaluating Method Applications.................................................................................................................... 90
6.9.8
Evaluating Union Cases ................................................................................................................................. 91
6.9.9
Evaluating Field Lookups ............................................................................................................................... 91
6.9.10
Evaluating Array Expressions......................................................................................................................... 91
6.9.11
Evaluating Record Expressions ...................................................................................................................... 91
6.9.12
Evaluating Function Expressions.................................................................................................................... 91
6.9.13
Evaluating Object Expressions ....................................................................................................................... 91
6.9.14
Evaluating Definition Expressions.................................................................................................................. 92
6.9.15
Evaluating Integer For Loops ......................................................................................................................... 92
6.9.16
Evaluating While Loops ................................................................................................................................. 92
6.9.17
Evaluating Static Coercion Expressions.......................................................................................................... 92
6.9.18
Evaluating Dynamic Type-Test Expressions ................................................................................................... 92
6.9.19
Evaluating Dynamic Coercion Expressions..................................................................................................... 93
6.9.20
Evaluating Sequential Execution Expressions ................................................................................................ 93
6.9.21
Evaluating Try-with Expressions .................................................................................................................... 93
6.9.22
Evaluating Try-finally Expressions ................................................................................................................. 94
6.9.23
Evaluating AddressOf Expressions ................................................................................................................. 94
6.9.24
Values with Underspecified Object Identity and Type Identity ...................................................................... 94
7.
7.4
4
7.5
7.6
7.7
7.8
7.9
7.10
R ECORD P ATTERNS ................................................................................................................................................. 104
7.11
A RRAY P ATTERNS ................................................................................................................................................... 104
7.12
N ULL P ATTERNS ..................................................................................................................................................... 105
7.13
G UARDED P ATTERN R ULES ....................................................................................................................................... 105
8.
8.6.1
Primary Constructors in Classes................................................................................................................... 117
8.6.2
Members in Classes ..................................................................................................................................... 120
8.6.3
Additional Object Constructors in Classes.................................................................................................... 120
8.6.4
Additional Fields in Classes .......................................................................................................................... 122
8.7
8.8
8.9
8.10
D ELEGATE T YPE D EFINITIONS .................................................................................................................................... 126
8.11
E XCEPTION D EFINITIONS .......................................................................................................................................... 126
8.12
T YPE E XTENSIONS ................................................................................................................................................... 127
8.12.1
Imported CLI C# Extensions Members ......................................................................................................... 128
8.13
M EMBERS ............................................................................................................................................................ 129
8.13.1
Property Members....................................................................................................................................... 130
8.13.2
Method Members ........................................................................................................................................ 131
8.13.3
Curried Method Members ........................................................................................................................... 131
8.13.4
Named Arguments to Method Members..................................................................................................... 131
8.13.5
Optional Arguments to Method Members .................................................................................................. 132
8.13.6
Type-directed Conversions at Member Invocations..................................................................................... 134
8.13.7
Overloading of Methods .............................................................................................................................. 135
8.13.8
Naming Restrictions for Members ............................................................................................................... 136
8.13.9
Members Represented as Events................................................................................................................. 136
8.13.10
Members Represented as Static Members ............................................................................................. 137
8.14
A BSTRACT M EMBERS AND I NTERFACE I MPLEMENTATIONS ............................................................................................... 137
8.14.1
Abstract Members ....................................................................................................................................... 138
5
Zgłoś jeśli naruszono regulamin