The F# 4.0 Language Specification
Total Page:16
File Type:pdf, Size:1020Kb
The F# 4.0 Language Specification Note: This documentation is the specification of version 4.0 of the F# language, released in 2015-16. Discrepancies may exist between this specification and the 4.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 grateful for feedback on this specification, and on both the design and implementation of F#. You can submit feedback by opening issues, comments and pull requests at https://github.com/fsharp/fsfoundation/tree/gh-pages/specs/language-spec. The latest version of this specification can be found at fsharp.org. 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, Kevin Ransom, Chris Smith, Matteo Taveggia, Donna Malayeri, Wonseok Chae, Uladzimir Matsveyeu, Lincoln Atkinson, and others. Notice © 2005-2016 various contributors. Made available under the Creative Commons CC-by 4.0 licence. Product and company names mentioned herein may be the trademarks of their respective owners. Document Updates: Updates for F# 4.0, January 2016 Updates for F# 3.1 and type providers, January 2016 Edits to change version numbers for F# 3.1, May 2014 Initial updates for F# 3.1, June 2013 (see online description of language updates) Updated to F# 3.0, September 2012 Updated with formatting changes, April 2012 Updated with grammar summary, December 2011 Updated with glossary, index, and style corrections, February 2011 Updated with glossary, index, and style corrections, August 2010 Table of Contents 1. INTRODUCTION ................................................................................................................. 11 1.1 A FIRST PROGRAM ................................................................................................................... 11 1.1.1 Lightweight Syntax .......................................................................................................... 11 1.1.2 Making Data Simple ........................................................................................................ 12 1.1.3 Making Types Simple ....................................................................................................... 13 1.1.4 Functional Programming ................................................................................................. 14 1.1.5 Imperative Programming ................................................................................................ 15 1.1.6 .NET Interoperability and CLI Fidelity .............................................................................. 16 1.1.7 Parallel and Asynchronous Programming ....................................................................... 16 1.1.8 Strong Typing for Floating-Point Code ............................................................................ 17 1.1.9 Object-Oriented Programming and Code Organization .................................................. 17 1.1.10 Information-rich Programming ................................................................................... 19 1.2 NOTATIONAL CONVENTIONS IN THIS SPECIFICATION ...................................................................... 20 2. PROGRAM STRUCTURE ...................................................................................................... 23 3. LEXICAL ANALYSIS .............................................................................................................. 25 3.1 WHITESPACE ........................................................................................................................... 25 3.2 COMMENTS ............................................................................................................................ 25 3.3 CONDITIONAL COMPILATION ...................................................................................................... 26 3.4 IDENTIFIERS AND KEYWORDS ...................................................................................................... 26 3.5 STRINGS AND CHARACTERS ........................................................................................................ 28 3.6 SYMBOLIC KEYWORDS ............................................................................................................... 30 3.7 SYMBOLIC OPERATORS .............................................................................................................. 31 3.8 NUMERIC LITERALS ................................................................................................................... 31 3.8.1 Post-filtering of Adjacent Prefix Tokens .......................................................................... 32 3.8.2 Post-filtering of Integers Followed by Adjacent “..” ........................................................ 33 3.8.3 Reserved Numeric Literal Forms ...................................................................................... 33 3.8.4 Shebang ........................................................................................................................... 33 3.9 LINE DIRECTIVES ...................................................................................................................... 33 3.10 HIDDEN TOKENS ...................................................................................................................... 33 3.11 IDENTIFIER REPLACEMENTS ........................................................................................................ 34 4. BASIC GRAMMAR ELEMENTS.............................................................................................. 35 4.1 OPERATOR NAMES ................................................................................................................... 35 4.2 LONG IDENTIFIERS .................................................................................................................... 39 4.3 CONSTANTS ............................................................................................................................ 39 4.4 OPERATORS AND PRECEDENCE ................................................................................................... 40 4.4.1 Categorization of Symbolic Operators ............................................................................ 40 4.4.2 Precedence of Symbolic Operators and Pattern/Expression Constructs ......................... 41 5. TYPES AND TYPE CONSTRAINTS .......................................................................................... 43 5.1 CHECKING SYNTACTIC TYPES ...................................................................................................... 44 5.1.1 Named Types ................................................................................................................... 45 5.1.2 Variable Types ................................................................................................................. 45 5.1.3 Tuple Types ...................................................................................................................... 46 5.1.4 Array Types ...................................................................................................................... 46 2 5.1.5 Constrained Types ........................................................................................................... 47 5.2 TYPE CONSTRAINTS .................................................................................................................. 47 5.2.1 Subtype Constraints ........................................................................................................ 47 5.2.2 Nullness Constraints ........................................................................................................ 48 5.2.3 Member Constraints ........................................................................................................ 48 5.2.4 Default Constructor Constraints ...................................................................................... 49 5.2.5 Value Type Constraints .................................................................................................... 49 5.2.6 Reference Type Constraints ............................................................................................. 50 5.2.7 Enumeration Constraints ................................................................................................. 50 5.2.8 Delegate Constraints ....................................................................................................... 50 5.2.9 Unmanaged Constraints .................................................................................................. 51 5.2.10 Equality and Comparison Constraints ......................................................................... 51 5.3 TYPE PARAMETER DEFINITIONS .................................................................................................. 52 5.4 LOGICAL PROPERTIES OF TYPES ................................................................................................... 53 5.4.1 Characteristics of Type Definitions .................................................................................