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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:40:58+00:00 2026-06-16T10:40:58+00:00

This is related to this question . I’d like to create a generic wrapper

  • 0

This is related to this question.

I’d like to create a generic wrapper class:

public abstract class Wrapper<T>
{
    internal protected T Wrapped { get; set; }
}

With the following extensions:

public static class WrapperExtensions
{
    public static W Wrap<W,T>(this T wrapped) where W:Wrapper<T>,new()
    {
        return new W {Wrapped = wrapped};
    }

    public static T Unwrap<T>(this Wrapper<T> w)
    {
        return w.Wrapped;
    }
}

Now assume a concrete Wrapper:

public class MyIntWrapper : Wrapper<int>
{
    public override string ToString()
    {
        return "I am wrapping an integer with value " + Wrapped;
    }
}

I would like to call the Wrap extension like this:

MyIntWrapper wrapped = 42.Wrap<MyIntWrapper>(); 

This is not possible because in c# we need to provide both type arguments to the Wrap extension.
(it’s all or nothing)

Apparently partial inference is possible in F#.

How would the above code look in F#?

Would it be possible to use it from C#?

  • 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-06-16T10:40:59+00:00Added an answer on June 16, 2026 at 10:40 am

    Apparently partial inference is possible in F#.

    Yes, only W would need to be specified in your example. T will be inferred.

    How would the above code look in F#?

    [<AbstractClass>]
    type Wrapper<'a>() =
        [<DefaultValue>]
        val mutable internal Wrapped : 'a
    
    let Wrap<'W,'T when 'W :> Wrapper<'T> and 'W: (new: unit -> 'W)> (wrapped: 'T): 'W = 
        let instance = new 'W()
        instance.Wrapped <- wrapped
        instance
    
    let Unwrap (w: Wrapper<_>) = w.Wrapped
    
    type MyIntWrapper() =
        inherit Wrapper<int>()
        override this.ToString() = 
            sprintf "I'm wrapping an integer with value %d" this.Wrapped
    

    And you can call Wrap from F# interactive this way

    > let wrapped = Wrap<MyIntWrapper,_> 5;;
    val wrapped : MyIntWrapper = I'm wrapping an integer with value 5
    

    In my opinion this is not very idiomatic in F#, I would rather use Discriminated Unions and Pattern Matching for wrapping/unwrapping but I don’t know exactly what’s your specific case.

    Would it be possible to use it from C#?

    Sure, but if you call Wrap from C# you’re back to C# type inference and will have to specify the second type parameter.

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

Sidebar

Related Questions

See this related question on more generic use of the Boost Random library. My
Similar to this related question , I would like to perform a parallel map
This question is an extension of this Related question . Taking Derick's advice, I
I found this related question: How do I use composition with inheritance? I would
I have searched here, GooBingHooVista'd the world and read this related question for VS
This is a related question to this but with EXISTS instead of IN SELECT
This is a related question to one I posted earlier... I'm trying to sum
This is a related question to: Android NFC start service In Android, can an
This is a related question to one I posted earlier today, I was initially
Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility,

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.