Category Archives: F#

ACCU: Introducing Concepts for C++

Overload 129October 2015’s edition of Overload includes an excellent article by Andrew Sutton on Concepts for C++. I first heard about Concepts at the ACCU 2013 conference and it’s exciting to hear that they have reached Technical Specification stage.

There’s no doubt in my mind that concepts will play a huge role for those involved in generic programming. In F#, we already have the ability to state constraints on template arguments e.g. to ensure that ‘T satisfies the comparison constraint:

type Container<'T when 'T : comparison>( items : 'T[] ) =
    // ...

However, I wasn’t expecting the ability to use concepts for placeholders (so that, once you’ve defined a concept, you can use it where you might have used auto to gain the benefits of type deduction without the loss of information).

Leave a comment

Filed under C++, F#, Programming

Ten reasons (not) to use a functional programming language

Amusing tongue-in-cheek rant by FSharpForFunAndProfit about why you *should* use a functional programming language.

Leave a comment

Filed under F#

How to call methods with byref parameters from F#

When consuming C# libraries in F# projects, out parameters will be interpreted in F# with type byref. For example:

    public class Arithmetic
    {
        public static void sumAndDifference(
            int a, int b, out int sum, out int difference)
        {
            sum = a + b;
            difference = a - b;
        }
    }

Here, sumAndDifference has the following signature when the library is referenced from an F# project:
ByRef
The literal way to call the method is as follows, declaring reference value up-front and de-referencing them for use after the method call:

    let sum : int ref = ref 1
    let difference : int ref = ref 2
    Arithmetic.sumAndDifference(5, 3, sum, difference )
    printfn "Sum = %d, Difference = %d" !sum !difference

But F# also provides a much more concise syntax, allowing the out parameters to appear on the left hand side of an assignment:

    let sum, difference = Arithmetic.sumAndDifference(5, 3)
    printfn "Sum = %d, Difference = %d" sum difference

This syntax is so much cleaner and avoids any mention of references.

1 Comment

Filed under F#, Programming