Sorry for the vague description, couldn’t find a better way to plain it.
I’m starting with F# and like many others I’m translating my solved Euler problems to F#. I like to run my code using tests and also I like the FsUnit style. With the help of the given example I did this:
open System
open NUnit.Framework
open FsUnit
type EulerProblem() =
member this.problem1 = Seq.init 999 (fun n -> n + 1)
|> Seq.filter (fun n -> n % 3 = 0 || n % 5 = 0)
|> Seq.sum
[<TestFixture>]
type ``Given an Euler problem`` () =
let euler = new EulerProblem()
[<Test>] member test.
``when I try to solve #1 should return [the magic number]`` ()=
euler.problem1 |> should equal [the magic number]
This works, but I cannot understand what the period after the test method does. If I take it away the compiler complaints saying:
This instance member needs a parameter to represent the object being invoked. Make the member static or use the notation ‘member x.Member(args)
Sorry if this is trivial, and maybe i’m not using the correct wording but couldn’t get an answer with google.
Thanks
If you are familiar with C#, or Java, or C++, you can refer to a class instance using the
thisreserved word within instance members. In F#, you must explicitly give a name to the class instance for each member definition and in the FsUnit example, the name given is merelytestbut it’s not actually used. You could just the same have written the test method asBut note that these days, both xUnit.net and NUnit allow applying their
[<Fact>]and[<Test>]attributes respectively for marking tests on let bound functions within modules and without needingTestFixtures and such, which is much better suited for F#. So, for example, the test you gave can and in my opinion ought to be written as:Moreover, you probably don’t want to create your problem solutions as members of a
typelikeEulerProblem, rather as functions within a module.