F# – Basic Syntax

  • Post author:
  • Post category:F#
  • Post comments:2 Comments
F# - Basic Syntax

F# Basic Syntax has seen the basic structure of an F# program, so it will be easy to understand other basic building blocks of the F# programming language.

Tokens in F# Basic Syntax

An F# program consists of various tokens. A token could be a keyword, an identifier, a constant, a string literal, or a symbol. We can categorize F# tokens into two types −

  • Keywords
  • Symbol and Operators

F# Keywords

The following table shows the keywords and brief descriptions of the keywords. We will discuss the use of these keywords in subsequent chapters.

KeywordDescription
abstractIndicates a method that either has no implementation in the type in which it is declared or that is virtual and has a default implementation.
andUsed in mutually recursive bindings, in property declarations, and with multiple constraints on generic parameters.
asUsed to give the current class object an object name. Also used to give a name to a whole pattern within a pattern match.
assertUsed to verify code during debugging.
baseUsed as the name of the base class object.
beginIn verbose syntax, indicates the start of a code block.
classIn verbose syntax, indicates the start of a class definition.
defaultIndicates an implementation of an abstract method; used together with an abstract method declaration to create a virtual method.
delegateUsed to declare a delegate.
doUsed in looping constructs or to execute imperative code.
doneIn verbose syntax, indicates the end of a block of code in a looping expression.
downcastUsed to convert to a type that is lower in the inheritance chain.
downtoIn a for expression, used when counting in reverse.
ElifUsed in conditional branching. A short form of else if.
elseUsed in conditional branching.
endIn type definitions and type extensions, indicates the end of a section of member definitions.In verbose syntax, used to specify the end of a code block that starts with the begin keyword.
exceptionUsed to declare an exception type.
externThis indicates that a declared program element is defined in another binary or assembly.
falseUsed as a Boolean literal.
finallyUsed together with try to introduce a block of code that executes regardless of whether an exception occurs.
forUsed in looping constructs.
funUsed in lambda expressions, also known as anonymous functions.
functionUsed as a shorter alternative to the fun keyword and a match expression in a lambda expression that has pattern matching on a single argument.
globalUsed to reference the top-level .NET namespace.
ifUsed in conditional branching constructs.
inUsed for sequence expressions and, in verbose syntax, to separate expressions from bindings.
inheritUsed to specify a base class or base interface.
inlineUsed to indicate a function that should be integrated directly into the caller’s code.
interfaceUsed to declare and implement interfaces.
internalUsed to specify that a member is visible inside an assembly but not outside it.
lazyUsed to specify a computation that is to be performed only when a result is needed.
letUsed to associate, or bind, a name to a value or function.
let!Used in asynchronous workflows to bind a name to the result of an asynchronous computation, or, in other computation expressions, used to bind a name to a result, which is of the computation type.
matchUsed to the branch by comparing a value to a pattern.
memberUsed to declare a property or method in an object type.
moduleUsed to associate a name with a group of related types, values, and functions, to logically separate it from other code.
mutableUsed to declare a variable, that is, a value that can be changed.
namespaceUsed to associate a name with a group of related types and modules, to logically separate it from other code.
newUsed to declare, define, or invoke a constructor that creates or that can create an object. Also used in generic parameter constraints to indicate that a type must have a certain constructor.
notNot actually a keyword. However, not struct in combination is used as a generic parameter constraint.
nullIndicates the absence of an object. Also used in generic parameter constraints.
ofUsed in discriminated unions to indicate the type of categories of values, and in delegate and exception declarations.
openUsed to make the contents of a namespace or module available without qualification.
orUsed with Boolean conditions as a Boolean or operator. Equivalent to ||.Also used in member constraints.
overrideUsed to implement a version of an abstract or virtual method that differs from the base version.
privateRestricts access to a member to code in the same type of module.
publicAllows access to a member from outside the type.
recUsed to indicate that a function is recursive.
returnUsed to indicate a value to provide as the result of a computation expression.
return!Used to indicate a computation expression that, when evaluated, provides the result of the containing computation expression.
selectUsed in query expressions to specify what fields or columns to extract. Note that this is a contextual keyword, which means that it is not actually a reserved word and it only acts as a keyword inappropriate context.
staticUsed to indicate a method or property that can be called without an instance of a type, or a valued member that is shared among all instances of a type.
structUsed to declare a structure type. Also used in generic parameter constraints. Used for OCaml compatibility in module definitions.
thenUsed in conditional expressions. Also used to perform side effects after object construction.
toUsed in for loops to indicate a range.
trueUsed as a Boolean literal.
tryUsed to introduce a block of code that might generate an exception. Used together with or finally.
typeUsed to declare a class, record, structure, discriminated union, enumeration type, unit of measure, or type abbreviation.
upcastUsed to convert to a type that is higher in the inheritance chain.
useUsed instead of let for values that require Dispose to be called to free resources.
use!Used instead of let! in asynchronous workflows and other computation expressions for values that require Dispose to be called to free resources.
ValUsed in a signature to indicate a value, or in a type to declare a member, in limited situations.
voidIndicates the .NET void type. Used when interoperating with other .NET languages.
whenUsed for Boolean conditions (when guards) on pattern matches and to introduce a constraint clause for a generic type parameter.
whileIntroduces a looping construct.
withUsed together with the match keyword in pattern matching expressions. Also used in object expressions, record copying expressions, and type extensions to introduce member definitions, and to introduce exception handlers.
yieldUsed in a sequence expression to produce a value for a sequence.
yield!Used in a computation expression to append the result of a given computation expression to a collection of results for the containing computation expression.

Some reserved keywords came from the OCaml language −

asrlandLORLSLLSRLuxormodsig

Some other reserved keywords are kept for future expansion of F#.

atomicbreakcheckedcomponentconstconstraintconstructor
continueeagereventexternalfixedfunctorinclude
methodmixinobjectparallelprocessprotectedpure
sealedtail-calltraitvirtualvolatile

Comments in F#

F# provides two types of comments −

  • One line comment starts with // symbol.
  • Multi line comment starts with (* and ends with *).

A Basic Program and Application Entry Point in F#

Generally, you don’t have any explicit entry point for F# programs. When you compile an F# application, the last file provided to the compiler becomes the entry point and all top-level statements in that file are executed from top to bottom.

A well-written program should have a single top-level statement that would call the main loop of the program.

A very minimalistic F# program that would display ‘Hello World’ on the screen

(* This is a comment *)
(* Sample Hello World program using F# *)
printfn "Hello World!"

When you compile and execute the program, it yields the following output −

Hello World!

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply