Phil Trelford's Array
POKE 36879, 255

Strangeloop 2012

September 26, 2012 07:38 by Phil

This year I had the privilege of attending and speaking at the Strangeloop conference in St Louis. The venues, the Union Street Marriot (a National Historic Landmark), and the Peabody Opera House were spectacular.


The range of sessions and speakers was literally mind boggling. Some of my favourites:

The breakfast hallway conversations were friendly and insightful, thanks to everyone for all the great feedback on my talk.

F# for Trading

Many people in the community use F# for Trading, and not just developers, traders too. Community matters in a language. The talk looked at some of the reasons people choose to use F# in Trading and some of the projects they have built to support their efforts, like Type Providers for Excel and the R statistical programming language.

The talk was recorded so expect a video to appear on InfoQ at some point in the future.

References (4.15 mb)

Categories: .Net | BDD | C# | C++ | F# | JavaScript | Twitter
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

F# eye for the C++ guy

September 19, 2012 00:23 by phil

As a C++11 programmer you’ve probably played with type inference and lambda functions and may now feel curious about trying a functional first programming language like F#. F# is a first class .Net programming language that ships with Visual Studio. Although C# shares the familiar moustache based syntax of C++, you may like me actually find F# more familiar in terms of power features like immutability, function inlining and even sprintf .

Comparing some of the good parts of C++11 with C# and F#:

C++11 C# F#
formerly B. Stroustrup
Anders Hejlsberg* Don Syme
Statically typed Yes Yes
Generic programming Template classes, methods & functions
e.g. list<T>
Generic types & methods
e.g. List<T>
Generic types, methods & functions
e.g. list<T>
Type aliasing typedef keyword
e.g. typedef list<int> xs;
using directive
e.g. using xs=List<T>;
type keyword
e.g. type xs = list<int>
Type inference auto keyword
on local & global variables
var keyword
on local variables
let keyword
on values, variables, parameters & return values
Math functions Built-in
e.g. abs, min, sin, pow
System.Math class
e.g. Math.Abs
e.g. abs, min, sin, pown
String format sprintf function 
e.g. sprintf(“%d”, 1)
String.Format method
sprintf function
e.g. sprintf “%d” 1
Inline functions inline keyword
N/A inline keyword
Immutability const correctness
mutable keyword
readonly fields Immutable by default
mutable keyword
Immutable collections N/A N/A Built-in
e.g. list, set, map
Agents Concurrency runtime
Agents library
DevLabs project
TPL Dataflow
Resource management Resource acquisition is initialization (RAII) Explicit scope with using statement on IDisposable objects Automatic scope with use keyword or explicit scope with using function
Interop COM & P/Invoke P/Invoke & COM P/Invoke & COM

* Note: Anders Hejlsberg is reported to be currently working on JavaScript tooling

Resource management

In .Net, garbage collection replaces C++’s delete method for reclaiming memory. For resources like files or database connections that implement IDisposable, .Net can feel a bit more clumsy requiring explicit disposal, over using C++ with  RAII.

In C# to determine if a class implements IDisposable you must look at the implementation, and then explicitly dispose the object with either a using block or calling the Dispose method.

using (Font font1 = new Font("Arial", 10.0f))
    byte charset = font1.GdiCharSet;

In F# you can tell if a class implements IDisposable by hovering over it in Visual Studio. Also types implementing IDisposable must be created using the new keyword, which gives an explicit code indication. Finally F# has a use keyword which can be used in place of the let keyword for IDisposable types and automatically calls Dispose when the object is out of scope.

use font1 = new Font("Arial", 10.0f)
let charset = font1.GdiCharSet


Like C++, F# provides a printf and sprintf function. What may be a little more surprising is that in F# they are type safe:


F# will generate a compile time error if the number or type of parameters don’t match.


Although not required F# supports signatures which are akin to C++ header files and useful for libraries (for example F#’s built-in libraries uses them), particularly for separating code documentation examples from the actual implementation code.


If you’re interested in augmenting your high performance C++ code with some high level functional code, look beyond the syntax and you may find F# more familiar and feature rich.

Categories: C++ | C# | F# | .Net
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Scrap your Boilerplate

September 4, 2012 00:16 by phil

Boilerplate is any seemingly repetitive code that shows up again and again, like getting and setting instance variables, but seems like it ought to be much simpler.

Jon Harrop recently commented on an MSDN article on Functional Programming in C++:

(from Figure 8) 60 lines of "functional" C++ in 1 line of F#: type Immutable = {d: float; s: string}…

60 lines is a lot of boilerplate! Here Jon defines a 1 line record type to express a simple immutable type.


Java and C# reduced the repetition of header files in C++. However C# code is still full of boilerplate, to the point where third party Visual Studio plugins like Resharper have become the de-facto standard for C# developers as a band-aid over the repetitive strain.

F# on the other hand is quite terse, which not only reduces the chances of RSI. it can also make the code easier to read and maintain.

Here’s 3 short examples of how F# avoids some of the boilerplate pitfalls of C#.

1. Constructors

It is common to inject dependencies into a class via the constructor. In C# these dependencies are then typically captured as fields for use by member methods:

public class VerySimpleStockTraderImpl : IAutomatedStockTrader 
    private readonly IStockAnalysisService analysisService;
    private readonly IOnlineBrokerageService brokerageService;
    public VerySimpleStockTraderImpl(
        IStockAnalysisService analysisService,
        IOnlineBrokerageService brokerageService) 
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    public void ExecuteTrades() 
        // ...

Notice how both the analysisService and brokerageService names are referenced 3 times. I call this the local government pattern™ as everything needs to be filled out in triplicate.

F# classes specify the primary constructor as a closure over the class members so the arguments are captured in one hit:

type VerySimpleStockTraderImpl
         brokerageService:IOnlineBrokerageService) =
    member this.ExecuteTrades() = () // ...

2. Properties

Other parameters to constructors may specify initial values for properties. C# 3 introduced auto-implemented properties to help reduce the boilerplate, but it is still quite verbose:

public class OrderLine
    public OrderLine(decimal price, int quantity)
        this.Price = price;
        this.Quantity = quantity;

    public decimal Price { get; private set; }
    public int Quantity { get; set; }

F# lets you specify the initial value for a property at the property definition:

type OrderLine(price:decimal, quantity:int) =
    member val Price = price
    member val Quantity = quantity with get, set

The sample above uses the new F# 3 auto-property syntax.

3. Out parameters

In C# if you want to return more than one value from a function you must resort to out parameters:

string value = "";
if (openWith.TryGetValue("tif", out value))
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
    Console.WriteLine("Key = \"tif\" is not found.");

F# lets you return any number of values. C# methods using out parameters can be called from F# as if they returned multiple parameters:

match openWith.TryGetValue("tif") with
| true, value -> printfn "For key = \"tif\", value = %A." value
| false, _ -> printfn "Key = \"tif\" is not found."


F# class and property syntax dramatically reduce the boilerplate over C# class definitions. Returning multiple values is easy in F# and there’s even support for methods using C#’s out parameters. Here I’ve presented just 3 examples that help scrap the boilerplate, but this is only the tip of the iceberg.

Interested? Try F#

Categories: C++ | C# | F# | .Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed