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 6911097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:54:29+00:00 2026-05-27T08:54:29+00:00

Given the following code: let DisplayImpl logger data = data |> Seq.iter logger printfn

  • 0

Given the following code:

let DisplayImpl logger data =
    data |> Seq.iter logger
    printfn ""

let Working =
    DisplayImpl (printfn "%O") [1;2;3]
    DisplayImpl (printfn "%O") ["a";"b";"c"]

let NotWorking display =
    display (printfn "%O") [1;2;3]
    display (printfn "%O") ["a";"b";"c"]
                            ~~~ ~~~ ~~~

The last line gives the error: This expression was expected to have type int but here has type string

I thought the following might work, but it doesn’t:

let StillNotWorking (display: ('a -> unit) -> seq<'a> -> unit) =

My question is, how do I define the NotWorking function so that the display parameter stays generic within the function?

  • 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-27T08:54:29+00:00Added an answer on May 27, 2026 at 8:54 am

    Functions that are passed as arguments to other functions (like your display) cannot be themselves polymorphic in F#. They can use the generic type parameter ('a etc.), but the actual type for this parameter is specified when the main function (NotWorking in your case) is called. This means that you can only call display with single actual type used for the type variable 'a in the body of NotWorking.

    As a workaround, you can use an interface with a generic method:

    type Displayer = 
      abstract Display : (obj -> unit) -> 'T list -> unit
     
    let NotWorking (display:Displayer) = 
        display.Display (printfn "%O") [1;2;3] 
        display.Display (printfn "%O") ["a";"b";"c"] 
    

    The method Display of the interface is itself generic method, so you can call that method multiple times with different type arguments (int in the first case and string in the second).

    However, I didn’t find this a limitation when writing normal code in F# very often, so maybe there is an easier solution for your problem (possibly, taking a non-generic IEnumerable or something simple like that – or obj list as in the answer from John). If you give more details about your actual code, that would be useful.

    Some background, just in case you’re interested in theoretical details, but none of this is really something that would be important in day-to-day real world F# programming. Anyway –

    This is possible in other langauges like Haskell and the mechanism that allows it is called universal types. When you have a polymorphic function in F#, it essentially means that that the scope of the type variables is the entire function, so ('a -> unit) -> unit can be viewed as forall 'a . ('a -> unit) -> unit.

    When you call the function, you need to specify what 'a is and that cannot be changed (i.e. you can’t use the function 'a -> unit that you get as an argument with two different types for 'a once 'a is fixed).

    Using universal types, you can write forall yourself, so you can say that the type is:
    (forall 'a . 'a -> unit) -> unit. Now the generic parameter 'a is only linked to the function that you’ll get as an argument. The type of the function given as an argument is now itself a generic function and so you can call it with different types standing for 'a.

    PS: Value restriction is a different problem – that essentially means that F# cannot make things that are not syntactic functions generic, but in your example, you’re writing syntactic functions, so that’s not the issue here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: let bar = lazy( printfn bar ()) let foo =
So, given then following code type MyClass () = let items = Dictionary<string,int>() do
Given the following code (that doesn't work): while True: # Snip: print out current
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
given the following code: import ctypes ip=192.168.1.1 thisdll = ctypes.cdll['aDLL'] thisdll.functionThatExpectsAnIP(ip) how can I
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code,the hover function works correctly, but when clicked, loses the clicked_no_event_box
Given the following code: using (var client = new WebClient()) { string url =
Given the following code, is there a way I can call class A's version
Given the following code $c= new SoapClient('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL'); $usa = USD; $eng = GBP; doing

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.