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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:14:11+00:00 2026-05-23T14:14:11+00:00

In the following code Seq.generateUnique is constrained to be of type ((Assembly -> seq<Assembly>)

  • 0

In the following code Seq.generateUnique is constrained to be of type ((Assembly -> seq<Assembly>) -> seq<Assembly> -> seq<Assembly>).

open System
open System.Collections.Generic
open System.Reflection

module Seq =
  let generateUnique =
    let known = HashSet()
    fun f initial ->
      let rec loop items = 
        seq {
          let cachedSeq = items |> Seq.filter known.Add |> Seq.cache
          if not (cachedSeq |> Seq.isEmpty) then
            yield! cachedSeq
            yield! loop (cachedSeq |> Seq.collect f)
        }
      loop initial

let discoverAssemblies() =
  AppDomain.CurrentDomain.GetAssemblies() :> seq<_>
  |> Seq.generateUnique (fun asm -> asm.GetReferencedAssemblies() |> Seq.map Assembly.Load)

let test() = printfn "%A" (discoverAssemblies() |> Seq.truncate 2 |> Seq.map (fun asm -> asm.GetName().Name) |> Seq.toList)
for _ in 1 .. 5 do test()
System.Console.Read() |> ignore

I’d like it to be generic, but putting it into a file apart from its usage yields a value restriction error:

Value restriction. The value
‘generateUnique’ has been inferred to
have generic type val
generateUnique : ((‘_a -> ‘_b) -> ‘_c
-> seq<‘_a>) when ‘_b :> seq<‘_a> and ‘_c :> seq<‘_a> Either make the
arguments to ‘generateUnique’ explicit
or, if you do not intend for it to be
generic, add a type annotation.

Adding an explicit type parameter (let generateUnique<'T> = ...) eliminates the error, but now it returns different results.

Output without type parameter (desired/correct behavior):

["mscorlib"; "TEST"]
["FSharp.Core"; "System"]
["System.Core"; "System.Security"]
[]
[]

And with:

["mscorlib"; "TEST"]
["mscorlib"; "TEST"]
["mscorlib"; "TEST"]
["mscorlib"; "TEST"]
["mscorlib"; "TEST"]

Why does the behavior change? How could I make the function generic and achieve the desired behavior?

  • 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-05-23T14:14:12+00:00Added an answer on May 23, 2026 at 2:14 pm

    generateUnique is a lot like the standard memoize pattern: it should be used to calculate memoized functions from normal functions, not do the actual caching itself.

    @kvb was right about the change in the definition required for this shift, but then you need to change the definition of discoverAssemblies as follows:

    let discoverAssemblies =
      //"memoize"
      let generator = Seq.generateUnique (fun (asm:Assembly) -> asm.GetReferencedAssemblies() |> Seq.map Assembly.Load)
    
      fun () ->
          AppDomain.CurrentDomain.GetAssemblies() :> seq<_>
          |> generator
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code that populates a System.Collections.Generic.List I don't like it so I
Consider the following code to demonstrate the question: let sequence = Seq.initInfinite (fun _
Given the following code: let DisplayImpl logger data = data |> Seq.iter logger printfn
open System type Foo() = interface Collections.IEnumerable with member x.GetEnumerator () = null type
I have the following code implementation of Breadth-First search. trait State{ def successors:Seq[State] def
I can create the desired date sequences using the following code: DateTwoWeeks1 <- seq(as.Date(2010/8/6),
A bit of a sytax question... I have the following code in F# let
The following code appears to force line-seq to read 4 lines from file .
I am using the following code which I have found online def c_int_binary_search(seq,t): #
I have the following code: try { conn.Open(); OracleCommand cmd = conn.CreateCommand(); cmd.CommandType =

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.