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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:57:20+00:00 2026-05-25T01:57:20+00:00

Is there a reason why I can use a generic function with different type

  • 0

Is there a reason why I can use a generic function with different type arguments when I pass it as a local value but not when passed as parameter?
For example:

let f = id

let g (x,y) = (f x, f y)

g ( 1, '2')

works fine, but if I try to pass the function as parameter

let g f (x,y) = (f x, f y)

g id ( 1, '2')

it fails because it takes the version f < int > and it tries to apply it twice.

I’ve found a workaround but it forces me to write twice the function I’m passing:

let g f1 f2 (x,y) = (f1 x, f2 y)

g id id ( 1, '2')

This is the second time I face this problem.

Why it behaves this way, it’s not supposed to be the same if the function is a local value or if it’s passed as parameter?

Is there a way to do this without duplicating the function?

A hack, maybe using explicit type constraints, inline magic, quotations?

  • 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-25T01:57:21+00:00Added an answer on May 25, 2026 at 1:57 am

    Here’s the inline magic.
    Let’s take kvb’s code and define a single gmap function that handles all cases:

    let inline gmap f (x, y) = f $ x, f $ y
    
    type One = One with static member ($) (One, x) = 1  // Example1 ConvertAll
    type Id  = Id  with static member ($) (Id , x) = x  // Example2 PassThrough
    
    type SeqSingleton  = SeqSingleton  with static member ($) (SeqSingleton , x) = seq [x]
    type ListSingleton = ListSingleton with static member ($) (ListSingleton, x) = [x]
    type ListHead      = ListHead      with static member ($) (ListHead, x) = List.head x
    
    // Usage
    let pair1 = gmap One ("test", true)
    let pair2 = gmap Id  ("test", true)
    let pair3 = gmap SeqSingleton  ("test", true)
    let pair4 = gmap ListSingleton ("test", true)
    let pair5 = gmap ListHead (["test";"test2"], [true;false])
    
    let pair6 = ("test", true) |> gmap ListSingleton |> gmap ListHead
    
    (* results
    val pair1 : int * int = (1, 1)
    val pair2 : string * bool = ("test", true)
    val pair3 : seq<string> * seq<bool> = (["test"], [true])
    val pair4 : string list * bool list = (["test"], [true])
    val pair5 : string * bool = ("test", true)
    val pair6 : string * bool = ("test", true)
    *)
    

    UPDATE

    It’s also possible to use the even more generic gmap function defined here then it will also work with n-uples (n < 9).

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

Sidebar

Related Questions

If so, can the image be animated? Is there a good reason not to
Is there any reason not to use the bitwise operators &, |, and ^
Is there any reason I can't use an older version of Data Access Application
Is there a specific reason that the EF requires new data providers and can't
Is there any reason something like this would not work? This is the logic
Is there any reason to use a varchar field instead of a date field
Is there a reason why most function definition in device driver in linux code
Is there a generic API call I can make to get a handle of
Please note that I am not talking about Generic Type parameters. For example in
apparently it works Can you name reasons beyond good practices not to give these

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.