SteelCityRKP
SteelCityRKP

Reputation: 169

Is there a way to correctly suppress an F# Type Constraint Warning?

I have a Type Constraint by design Explicitly defined for a set of functions that I intend to use Types with a corresponding List/Collection type. Even though I actually define the constraint it still gives me a warning that I cannot remove. This isn't critical, but I really so heavily on working through all the warnings and not being able to clear the list is a little annoying.

let domainValue (domain:'Domain when 'Domain :> 'ItemDomain list) //Address Type Constraint Warning ?
            : Value<'ItemDomain,'Domain> = (domain, (domain |> validDomainResult |> Some)) ||> value

Upvotes: 0

Views: 75

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243106

Your constraint is saying that the argument should be a type 'Domain such that 'Domain :> list<'ItemDomain>, i.e. a type that has list<'ItemDomain> as a base class.

However, list<'T> is a sealed class and so there can be no type other than list<'ItemDomain> that would satisfy the constraint - the only type that satisfies the constraint is list<'ItemDomain> and so you could as well simplify the type declaration and use:

let domainValue (domain:list<'ItemDomain>) = ...

It is worth noting that this is not the case for interfaces like seq<'T> and so the following gives no error:

let domainValue (domain:'Domain when 'Domain :> seq<'ItemDomain>) = ...

This will allow arguments that are any kind of collection of 'ItemDomain values including arrays, lists, etc. Also, you can write the same constraint more concisely using # type, which means the same thing:

let domainValue (domain:#seq<'ItemDomain>) = ...

Upvotes: 3

Related Questions