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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:29+00:00 2026-05-28T01:11:29+00:00

I’ve got the following example: type Stream (capacity) = let data = Array.zeroCreate capacity

  • 0

I’ve got the following example:

type Stream (capacity) =
    let data = Array.zeroCreate capacity
    member private s.position = ref 0
    static member private encoder = new Text.UTF8Encoding()
    static member private write (x, o, a : byte[]) = for i = 0 to 3 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256)
    static member private write (x, o, a : byte[]) = for i = 0 to 1 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256s)
    static member private write (x : string, o : int, a : byte[]) = Stream.encoder.GetBytes(x, 0, x.Length, a, o)
    static member format (x : int, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a
    static member format (x : int16, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a
    static member format (x : string, s) = let a = Array.create s 0uy in Stream.write(x, 0, a); a

First of all, sorry for the terribly messy code, I’m only a beginner in F#. As you may see the three format overloads differ only for their argument type, while their body is the same (although calling different overloads of write). Is it possible somehow to reduce the format functions to one, maybe inlined?

I apologize in case I’m completely missing the point here, but I couldn’t find much information about the matter.

  • 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-28T01:11:30+00:00Added an answer on May 28, 2026 at 1:11 am

    I think that the way you wrote it is probably the best option.

    If you don’t want to do boxing, then the overloaded write function is definitely needed, because the serialization needs to be implemented differently for different types. Using inline member also doesn’t work in this case, because inline function can only require a specific instance or static method on some value it gets, but not a specific overload.

    A reasonable solution that avoids some duplication is to define just a single overloaded function (i.e. write) and then pass it explicitly as an argument to other functions if they need it. You could write something like:

    type Stream (capacity) = 
        let data = Array.zeroCreate capacity 
        member private s.position = ref 0 
        static member private encoder = new Text.UTF8Encoding() 
        static member Write (x, o, a : byte[]) = 
            for i = 0 to 3 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256) 
        static member Write (x, o, a : byte[]) = 
            for i = 0 to 1 do a.[o + i] <- byte((x >>> 24 - i * 8) % 256s) 
        static member Write (x : string, o : int, a : byte[]) = 
            Stream.encoder.GetBytes(x, 0, x.Length, a, o) |> ignore
        // Format takes a writer function 'f' as the first argument
        static member Format (x, s) f = let a = Array.create s 0uy in f(x, 0, a); a 
    
    // When you call 'Format' with 'Stream.Write' as an argument, 
    // the compiler chooses the right overload for you
    Stream.Format (1s, 100) Stream.Write 
    Stream.Format ("foo", 100) Stream.Write 
    

    This avoids some code duplication at the definition side, but it makes the usage longer. If you don’t need many functions like Format, then the best way may be to define an overload with some code duplication as you did originally.

    Regarding inline, you can use that to specify that a type of the argument should implement some specific member (either instance or static), but you cannot say that there should be a specific overload. If you wrote wrappers for all three types (int16, string, …) that had a static member Write, then you could write:

    let inline format< ^T when ^T : 
        (static member Write : ^T * int * byte[] -> unit)> (value:^T) size =
      let a = Array.create size 0uy
      (^T : (static member Write : ^T * int * byte[] -> unit) (value, 0, a)) 
      a
    

    … but that’s even more complex solution that also requires writing some additional code when calling format, so I wouldn’t really use that approach (but it may be useful to know that it exists).

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and

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.