Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:48:37+00:00 2026-05-27T01:48:37+00:00

I have a bunch of F# functions that implement different algorithms for the same

  • 0

I have a bunch of F# functions that implement different algorithms for the same input, kind of like the Strategy pattern. To pick the right strategy, I want to pattern match on the input argument and return the function as a value :

let equalStrategy points : seq<double> =
    ...

let multiplyStrategy factor (points: seq<double>) =
    ...

let getStrategy relationship = 
    match relationship with
        | "="  ->  equalStrategy
        | "*5" ->  multiplyStrategy 5.0
        | _    -> raise (new System.NotImplementedException(" relationship not handled"))

Now I want to write some unit tests to make sure that I return the right strategy, so I tried something like this in nUnit :

    [<TestCase("=")>]
    [<Test>]
    member self.getEqualstrategy( relationship:string ) =            
        let strategy = getStrategy relationship

        Assert.AreEqual( strategy, equalStrategy )

Now I think the code is correct and will do what I want, but the assertion fails because functions don’t seem to have an equality operation defined on them. so my questions are :

(a) is there a way to compare 2 functions to see if they are the same, i.e. let isFoo bar = foo == bar, that I can use in an nUnit assertion?

or

(b) is there another unit testing framework that will do this assertion for me in F#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:48:37+00:00Added an answer on May 27, 2026 at 1:48 am

    Testing whether an F# function returned by your getStrategy is the same function as one of the funcions you defined is also essentially impossible.

    To give some details – the F# compiler generates a class that inherits from FSharpFunc when you return a function as a value. More importantly, it generates a new class each time you create a function value, so you cannot compare the types of the classes.

    The structure of the generated classes is something like this:

    class getStrategy@7 : FSharpFunc<IEnumerable<double>, IEnumerable<double>> {
      public override IEnumerable<double> Invoke(IEnumerable<double> points) {
        // Calls the function that you're returning from 'getStrategy'
        return Test.equalStrategy(points);
      }
    }
    
    // Later - in the body of 'getStrategy':
    return new getStrategy@7(); // Returns a new instance of the single-purpose class
    

    In principle, you could use Reflection to look inside the Invoke method and find which function is called from there, but that’s not going to be a reliable solution.

    In practice – I think you should probably use some other simpler test to check whether the getStrategy function returned the right algorithm. If you run the returned strategy on a couple of sample inputs, that should be enough to verify that the returned algorithm is the right one and you won’t be relying on implementation details (such as whether the getStrategy function just returns a named function or whether it returns a new lambda function with the same behaviour.

    Alternatively, you could wrap functions in Func<_, _> delegates and use the same approach that would work in C#. However, I think that checking whether getStrategy returns a particular reference is a too detailed test that just restricts your implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of overloaded functions that operate on certain data types such
I have a bunch of functions with signatures like: FUNCTION func1 (par1 IN VARCHAR2,
Like many 3d graphical programs, I have a bunch of objects that have their
I have a bunch of functions that I want to put in either a
I have a bunch of functions that map to and from some codes defined
Say i have a bunch of functions that will be using int = price;
I have a bunch of functions that take in either an array of objects,
I have a bunch of user-defined functions that are frequently used in mathematica. I
I have a bunch of legacy documents that are HTML-like. As in, they look
In a multithreaded application. I have a bunch of function that loop through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.