Skip to content

Nullness issue – Invalid type constraint resolution on ILookup #18334

@xperiandri

Description

@xperiandri
open System
open System.Collections.Generic
open System.Linq

module Extensions =

    type ILookup<'Key, 'Value when 'Key : not null> with

        static member Empty = Seq.empty<KeyValuePair<'Key, 'Value>>.ToLookup(_.Key, _.Value)

        member lookup.RemoveKey (key : 'Key) =
            lookup
            |> Seq.where (fun g -> 'Key.Equals(g.Key, key))
            |> Seq.collect (fun g -> g |> Seq.map (fun v -> KeyValuePair(g.Key, v)))
            |> _.ToLookup(_.Key, _.Value)

displays:

  • error FS0957: One or more of the declared type parameters for this type extension have a missing or wrong type constraint not matching the original type constraints on 'ILookup<_,_>'
  • error FS0340: The signature and implementation are not compatible because the declaration of the type parameter 'Key' requires a constraint of the form 'Key: not null
    while the required type constraint is present

Repro steps

use the uploaded class library project
WrongNonNullConditionRepro.zip

Expected behavior

No error

Actual behavior

  • error FS0957: One or more of the declared type parameters for this type extension have a missing or wrong type constraint not matching the original type constraints on 'ILookup<_,_>'
  • error FS0340: The signature and implementation are not compatible because the declaration of the type parameter 'Key' requires a constraint of the form 'Key: not null

Known workarounds

No

Related information

Provide any related information (optional):

  • .NET 8
  • Visual Studio 2022 17.13.1

Metadata

Metadata

Assignees

Labels

Area-NullnessIssues related to handling of Nullable Reference TypesBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions