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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:02+00:00 2026-05-16T16:32:02+00:00

Suppose I have a family of types which support a given member function, like

  • 0

Suppose I have a family of types which support a given member function, like the Property member below:

type FooA = {...} with

    member this.Property = ...

type FooB = {...} with

    member this.Property = ...

Suppose the member Property returns an integer for each of the the above types. Now, I wan to write a generic function that could do the following:

let sum (a: 'T) (b: 'U) = a.Property + b.Property

I am used to write the following in C++:

template<typename T, typename U>
int sum(T a, U b)
{
    return a.Property + b.Property;
}

How would be an equivalent implementation in F#?

  • 1 1 Answer
  • 1 View
  • 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-16T16:32:02+00:00Added an answer on May 16, 2026 at 4:32 pm

    It is possible to do this in F#, but it isn’t idiomatic:

    let inline sum a b = (^T : (member Property : int) a) + (^U : (member Property : int) b)
    

    In general, .NET generics are very different from C++ templates, so it’s probably worth learning about their differences first before trying to emulate C++ in F#. The general .NET philosophy is that operations are defined by nominal types rather than structural types. In your example you could define an interface exposing a Property property, which your two classes implement, and then your sum function would take instances of that interface.

    See http://msdn.microsoft.com/en-us/library/dd233215.aspx for more information on F# generics; for a brief comparison of .NET generics versus C++ templates see http://blogs.msdn.com/b/branbray/archive/2003/11/19/51023.aspx (or really anything that comes up when you Google “.NET generics C++ templates”).

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

Sidebar

Related Questions

suppose i have a query like this : $std_id = $_POST['std_id']; $name = $_POST['name'];
Suppose we have an xml like <texts> <text top=9 style=font-family:Helvetica;font-size:7; left=671>Page:</text> <text top=9 style=font-family:Helvetica;font-size:7;
Suppose we have a NAME table like this: PERSON_ID PART_TYPE VALUE and PERSON_ID is
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
Suppose we have the name written in any none-latin letters - languages, like Arabic,
I have this site: http://www.problemio.com which currently has a solid-colored background and the body
Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
I have an arraylist which is linked in a tree structure (a family tree
Suppose I have a FruitDetector class which takes in a string and returns the
Suppose I have the following code: type Vehicle = | Car of string *

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.