Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an interface member that returns void in F#

Tags:

Imagine the following interface in C#:

interface IFoo {
    void Bar();
}

How can I implement this in F#? All the examples I've found during 30 minutes of searching online show only examples that have return types which I suppose is more common in a functional style, but something I can't avoid in this instance.

Here's what I have so far:

type Bar() =
    interface IFoo with
        member this.Bar() =
            void

Fails with _FS0010:

Unexpected keyword 'void' in expression_.

like image 920
Drew Noakes Avatar asked Jun 18 '10 06:06

Drew Noakes


2 Answers

The equivalent is unit which is syntactically defined as ().

type Bar() =
    interface IFoo with
        member this.Bar () = ()
like image 73
ChaosPandion Avatar answered Nov 17 '22 00:11

ChaosPandion


For general info on F# types, see

The basic syntax of F# - types

From that page:

The unit type has only one value, written "()". It is a little bit like "void", in the sense that if you have a function that you only call for side-effects (e.g. printf), such a function will have a return type of "unit". Every function takes an argument and returns a result, so you use "unit" to signify that the argument/result is uninteresting/meaningless.

like image 35
Brian Avatar answered Nov 16 '22 23:11

Brian