Skip to content

What does the "where" do in interface Addable<T> where T : Addable<T> {} #1348

Answered by louthy
ThereIsNoPie asked this question in Q&A
Discussion options

You must be logged in to vote

Put this into a console project and try removing the constraint:

public interface Monoid<A>
    where A : Monoid<A>
{
    public static abstract A Zero { get; }
    public static abstract A Combine(A lhs, A rhs);

    public static virtual A Combine(IEnumerable<A> items)
    {
        var state = A.Zero;
        foreach (var item in items)
        {
            state = A.Combine(state, item);
        }
        return state;
    }
}

You'll see that the virtual method doesn't compile any more.

By enforcing the implementer of Monoid<A> to be the A itself we make static default behaviours available. Also, it's important that something like Option actually implements the behaviours of Functor<…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@ThereIsNoPie
Comment options

@louthy
Comment options

Answer selected by ThereIsNoPie
@louthy
Comment options

@louthy
Comment options

@ThereIsNoPie
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants