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

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: