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

  • Home
  • SEARCH
  • 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 8959383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:25:27+00:00 2026-06-15T15:25:27+00:00

Possible Duplicate: Type extension errors I would like to add an extension method in

  • 0

Possible Duplicate:
Type extension errors

I would like to add an extension method in F# to System.Collections.Generic.Dictionary. The trouble is that I cannot seem to get the type constraints right. I was hoping something like the following would work:

type Dictionary<'k, 'd when 'k : equality> with

   static member ofList (xs:list<'k * 'd>) : Dictionary<'k, 'd> =
       let res = new Dictionary<'k, 'd> ()
       for (k, d) in xs do
           res.Add (k, d)
       res

However, the compiler complains that my declaration differs from that of Dictionary. It does not produce that particular error when I leave out the equality constraint. But then it warns that it’s missing. Many thanks for any hints, preferably other that “turn the warning level down” 🙂

EDIT

Many thanks to KVB for providing the answer I wanted.

type Dictionary<'k, 'd> with

static member ofList (xs:list<'k * 'd>) : Dictionary<'k, 'd> =

    let res = new Dictionary<'k, 'd> (EqualityComparer<'k>.Default)
    for (k, d) in xs do
        res.Add (k, d)
    res

EDIT: Here is an example to better explain my reply to RJ. It shows that type arguments are optional when instantiating a type provided the compiler can infer them. It compiles without warnings or errors.

type System.Collections.Generic.Dictionary<'k, 'd> with
   static member test (dict:System.Collections.Generic.Dictionary<'k, 'd>) : bool =
        dict.Values |> List.ofSeq |> List.isEmpty


let test (x:System.Collections.Generic.Dictionary<'k, 'd>) =
    System.Collections.Generic.Dictionary.test x
  • 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-06-15T15:25:28+00:00Added an answer on June 15, 2026 at 3:25 pm

    If you need ofSeq functions for various collections, you might consider an approach similar to C# collection initializers. That is, make it work for any collection with an Add method. This also sidesteps your present problem.

    open System.Collections.Generic
    open System.Collections.Concurrent
    
    module Dictionary =
      let inline ofSeq s = 
        let t = new ^T()
        for k, v in s do
          (^T : (member Add : ^K * ^V -> ^R) (t, k, v)) |> ignore
        t
    
    module Collection =
      let inline ofSeq s = 
        let t = new ^T()
        for v in s do
          (^T : (member Add : ^V -> ^R) (t, v)) |> ignore
        t
    
    open Dictionary
    
    let xs = List.init 9 (fun i -> string i, i)
    let d1 : Dictionary<_,_> = ofSeq xs
    let d2 : SortedDictionary<_,_> = ofSeq xs
    let d3 : SortedList<_,_> = ofSeq xs
    
    open Collection
    
    let ys = List.init 9 id
    let c1 : ResizeArray<_> = ofSeq ys
    let c2 : HashSet<_> = ofSeq ys
    let c3 : ConcurrentBag<_> = ofSeq ys
    

    Interestingly, you can even limit it to collection types with a specific constructor overload. For example, if you wanted to use structural equality you could do:

    let t = (^T : (new : IEqualityComparer< ^K > -> ^T) (HashIdentity.Structural))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# dictionary type with unique keys and values I would like to
Possible Duplicate: No type inference with generic extension method Consider two methods: public static
Possible Duplicate: No type inference with generic extension method I have a generic function
Possible Duplicate: Determining the Type for a generic method parameter at runtime Generics in
Possible Duplicate: Extension Method Performance Will I face performance issues when I´m using extension
Possible Duplicate: get type of a generic parameter in java with reflection Java Generics
Possible Duplicate: Is it possible to create an extension method to format a string?
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
Possible Duplicate: .NET: Determine the type of this class in its static method How
Possible Duplicate: Overloading by return type Why can't I declare three member functions like

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.