Basic exception handling in F#

F# has a similar exception handling mechanism like in C# or Java. The most straightforward way to raise an F# exception is by way of the built-in “failwith” function which accepts an exception message:

failwith "This won't work"

Read more of this post

Close unmanaged resources in F#

There are some object types in .NET that represent so-called unmanaged resources. These resources cannot be automatically disposed of by the garbage collector. It is up to the programmer to close them in code otherwise they can remain open unintentionally. Typical examples include streams, such as a file stream, or network connections of some sort such as database or HTTP connections. The programmer should never forget to close these resources after using them.

C# has the using block for resources that implement the IDisposable interface. A using block makes the programmer’s job easier so that they don’t need to call the Close method actively. It will be called automatically at the end of the using block where the unmanaged resource goes out of scope.

F# has a similar construct called “use”. Let’s see an example with the MemoryStream object:

Read more of this post

Access modifiers in F#

F# has the public, private and internal keywords to indicate the access level of a member. If the access level is not specified then it is public by default like here:

namespace com.mycompany.domains.address

type Address = {
    street: string;
    city: string;
    number: int;
}

namespace com.mycompany.domains.order

open com.mycompany.domains.address

type Order (product:string, value: int, address:Address) =
    member this.Product = product
    member this.Value = value
    member this.Address = address

Read more of this post

Using the required qualified access attribute in F# to avoid name clashes

It can happen that a number of different modules have the same function name but have a different implementation. Here’s a module with a single function called “sum”:

module Top

let sum x y = x + y

Read more of this post

Modules in an F# project

F# supports grouping related code into namespaces and modules. Namespaces can only contain type declarations. They cannot contain values i.e. elements that are declared with the “let” keyword. Modules on the other hand can handle both types and values. Modules can also contain other modules, i.e. they can be nested.

Here’s a simple module with a type and a function:

Read more of this post

Changing the order of compilation in an F# project

If you start a new F# project in Visual Studio then the first source file you’ll get is Program.fs with a main function. Then say you add another source file called Domains.fs with the following namespace and type:

namespace com.mycompany.domains.book

type Book = {
    title: string;
    numberOfPages:int;
    author: string
} 
with member this.takesLongTimeToRead = this.numberOfPages > 500

The source file will at first land under Program.fs in the class hierarchy in Visual Studio:

Read more of this post

Namespaces in F#

The main purpose of namespaces in F# is to group related types in one container and avoid name clashes of types. Namespaces are declared using the “namespace” keyword like in many other popular languages. A significant difference between C#/Java and F# is that F# namespaces can only contain type declarations. They cannot hold any values, i.e. anything that’s declared using the “let” keyword. They are only containers for types, i.e. elements that are declared using the “type” keyword.

A single F# source file (.fs) can contain multiple namespaces. A namespace starts with the namespace declaration and ends either at the end of the source file or at the next namespace declaration. Here’s an example with two namespaces where each namespace has a single type:

Read more of this post

Discriminated unions and pattern matching in F#

Discriminated unions are similar to enumerations in F#. However, they offer more flexibility. A discriminated union is a set of types where each type can be different whereas each element of an enumeration must be an integer.

Here’s how we can declare a union of months:

type Months = January | February | March | April | May | June

We can then construct a couple of months as follows:

let jan = January
let feb = February
let apr = April
let jun = June

So it’s enough to use the name of the element in the union, we didn’t have to write the fully qualified name “Months.January” like in the case of an enumeration.

Read more of this post

Declaring a type as a Tuple in F#

We saw how Tuples work in F# in this post. Tuples are containers for various elements such as this one:

let myFirstTuple = (45, "hello world", 5., true, addThreeNumbers)

If you’d like to declare a type to be of Tuple then you can use the ‘*’ character as follows:

Read more of this post

Enumerations and pattern matching in F#

Enumeration types in F# are declared using the pipe symbol. Each element will have an integer value. Here’s an example with the first 6 months of the year:

type MonthsInFirstHalf = January = 1 | February = 2 | March = 3 | April = 4 | May = 5 | June = 6

We select an element using the dot notation which is the same as in C# or Java:

let april = MonthsInFirstHalf.April

The more interesting language construct is the F# equivalent of the switch statements to check for the value of the enumeration. In F# it’s called pattern matching.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

HarsH ReaLiTy

A Good Blog is Hard to Find

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: