Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Make assertions with Action<_> more friendly to F# by adding Func<_, _> and ignoring the result #2226

Closed
cmeeren opened this issue Jun 16, 2023 · 1 comment

Comments

@cmeeren
Copy link

cmeeren commented Jun 16, 2023

Background and motivation

I love FluentAssertions and use it for F#. However, F#'s overload resolution and type inference means that some parts of FluentAssertions are more difficult or verbose to use than with C#.

One thing that is annoying is that I want to be able to do this (note that I'm ignoring the because limitation mentioned in #2225 to make this point more clear):

elements.Should().AllSatisfy(fun e -> e.Should.Be(expected))

This fails to compile because AllSatisfy takes an Action, which means there must be no return value. But e.Should.Be(expected) returns a value of type AndConstraint<...Assertions>, and therefore compilation fails.

The ad-hoc, verbose workaround is to insert |> ignore in these places:

elements.Should().AllSatisfy(fun e -> e.Should.Be(expected) |> ignore)

The above compiles. But I have verified that if I add an extension like the below, the first example also compiles:

type GenericCollectionAssertions<'T> with

    member this.AllSatisfy(expected: Func<'T, 'TIgnored>) =
        this.AllSatisfy(fun x -> expected.Invoke(x) |> ignore)

My proposal is to change all relevant Action<T>-based overloads to instead use Func<T, TIgnored>, so that the "clean" F# code will compile as is. (Alternatively, new overloads could be added alongside existing ones.)

Alternative Concerns

No response

@cmeeren
Copy link
Author

cmeeren commented Jul 15, 2023

For what it's worth, I am no longer interested in this. As detailed in #2225 (comment), I am creating a brand new F# assertions library inspired by FluentAssertions. Feel free to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants