Implementing a C# interface with an F# object expression

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

Read more of this post

Advertisements

Implementing a C# interface with an F# type

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

We can implement this interface in an F# type as follows:

Read more of this post

Consuming a C# class in F#

F# and C# can work together pretty easily. Say that your domain classes are contained in a C# class library called Project.Domains. Let’s take the following Product class as an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Domains
{
	public class Product
	{
		public Product(string name, int price)
		{
			Name = name;
                        Price = price;
		}

		public string Name { get; }
		public int Price { get; }

		public double CalculateDiscountedPrice(double percentageOff)
		{
			double discount = Price * percentageOff;
			return Price - discount;
		}
	}
}

Read more of this post

Filtering exceptions on exception messages in F#

Here’s an example of pattern matching in a with clause in F#:

let testDotNetExceptions =
    try
        raise (new System.IndexOutOfRangeException("Your list is not large enough"))
    with
        | 😕 System.IndexOutOfRangeException as ex ->
            printfn "Out of range: %s" ex.Message
            0
        | 😕 System.Exception as ex ->
            printfn "Something bad has happened: %s" ex.Message
            0

Read more of this post

Handling .NET exceptions in F#

When working with .NET languages from F# it can happen that the F# code needs to deal with .NET exceptions from the System namespace. The way to deal with those exceptions is about the same as in the case of F# exceptions but pattern matching in the with clause is slightly different:

Read more of this post

Custom exceptions in F#

We can build custom exceptions in F# with the exception keyword. We can also assign multiple parameters to it in a tuple:

exception ProductNotFoundException of string * int * int

Read more of this post

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

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

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: