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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:18:46+00:00 2026-05-13T16:18:46+00:00

I have a curried function that I’d like it to support different types of

  • 0

I have a curried function that I’d like it to support different types of parameters, that are not on a inheritance relationship:

type MyType1 = A | B of float
type MyType2 = C | D of int

What I tried to do is:

let func x y =
    match (x, y) with
    | :? Tuple<MyType1, MyType1> -> "1, 1"
    | _ -> "..."

However this is not possible. F# complains:

The type ”a * ‘b’ does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion.

What is an elegant way to do this?

EDIT: Let me try to clarify this.

I have two similar, but distinct, types. I can very easily convert one type to another. I want to define a binary operation that will act on entities of those types, but I’d like to expose a single operation to the client.

That is, instead of providing:

let op11 (x : MyType1) (y : MyType1) = // do something useful
let op12 (x : MyType1) (y : MyType2) =
    // convert y to MyType1
    let y' = // ...
    // dispatch to op11
    op11 x y'
let op21 (x : MyType2) (y : MyType1) = // similar
let op22 (x : MyType2) (y : MyType2) = // similar

what I would like is to expose a single function to client code:

let op (x : obj) (y : obj) = // ...

This is like simulating the behavior of method overloading, but with curried functions.

  • 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-13T16:18:46+00:00Added an answer on May 13, 2026 at 4:18 pm

    Your code doesn’t work, because F# generalizes the type of arguments to a type parameter. I think you can’t dynamically test whether a type 'a * 'b can be converted to type MyType1 * MyType2 (though this is a bit confusing to me). In any case, you can write a function that takes two arguments of type obj and tests them separately using two :? patterns:

    type MyType1 = A | B of float 
    type MyType2 = C | D of int
    
    let func (x:obj) (y:obj) = 
        match (x, y) with 
        | (:? MyType1 as x1), (:? MyType1 as x2) -> 
            printfn "%A %A" x1 x2
        | _ -> 
            printfn "something else" 
    
    func A (B 3.0) // A B 3.0
    func A (D 42)  // something else
    

    Anyway, it would be interesting to know why do you want to do this? There may be a better solution…

    EDIT (2) So, from all 4 two-element combinations of T1 and T2, you want the function that can take 3. Is that correct (T1 * T1, T1 * T2 and T2 * T2)? In that case, you can’t write a fully safe curried function, because the type of second argument would “depend” on the type of first argument (if the first argument has a type T2, then the second argument also has to be T2 (otherwise it can be T1 too)).

    You can write a safe non-curried function that takes an argument of the following type:

    type MyArg = Comb1 of T1 * T1 | Comb2 of T1 * T2 | Comb3 of T2 * T2
    

    The type of the function would be MyArg -> string.
    If you want a curried function, you can define a type which allows you to use either T1 or T2 as both first and second argument.

    type MyArg = First of T1 | Second of T2
    

    Then, your curried function will be MyArg -> MyArg -> string. But note that if one combination of argument types is not allowed (if I understand you correctly, T2 * T1 shouldn’t be allowed). In this case, your function will simply have to throw an exception or something like that.

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

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I sometimes do what I call "meta-regexing" as follows: String… May 14, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer As you said, you have a border on your root… May 14, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer private static final int TAKE_PICTURE = 1; private Uri imageUri;… May 14, 2026 at 4:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.