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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:51:06+00:00 2026-05-30T23:51:06+00:00

Assume I created a type as follows: data RequestAck = RequestAck { ackOK ::

  • 0

Assume I created a type as follows:

data RequestAck = 
         RequestAck { ackOK :: Word32, ackMsgCode :: Word32 } 

I can see that it is 2 * 4 bytes big and make that a constant somewhere.

The only problems is that once I add a field to the type, I’d have to remember to update my constant.

Is there a function out there that will provide me with the size of a given type e.g., t -> Int?

The function that comes close to what I want is

gsize :: Data a => a -> Int

inside the Data.Generics.Schemes module, but I don’t want to have to make my type an instance of Data.

Is there a more generic solution out there?

To be sure, I’m looking for a function that operates on the static type, e.g., I don’t want to pass an instance, but the type itself.

  • 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-30T23:51:07+00:00Added an answer on May 30, 2026 at 11:51 pm

    This really depends on how you’re turning this into bytes.

    As a Word32, there is no fixed size. What you see as a Word32 could be an unapplied closure taking up hundreds of megabytes of space. Or it could be a simple boxed type (which would be larger than 4 bytes). Or it could be some kind of inline tagged type (in which case it depends on the platform). Or it could be elided (in which case it doesn’t exist).

    The only time when you can really say what size something like this is is when you convert to binary. If you’re doing this to interface with the FFI, you can use the sizeOf member of Foreign.Storable. Of course, you need to write a Storable instance for your type if you want to just apply sizeOf directly to it. If you’re serializing via Data.Binary… well, just serialize the thing, really. You usually don’t need to know the actual size beforehand (and if you do for a size header, just serialize the body you’re counting the size of into a temporary lazy bytestring, then take the size, then write the size and temp bytestring).

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

Sidebar

Related Questions

I am trying to achieve something roughly as follows: /* I assume that we
Basically I have defined a Tree data type which is defined as follows: data
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Let's assume, we create a stored procedure that is supposed to retrieve customer details
Assume that we have N erlang nodes, running same application. I want to share
It seems that when returning an object containing a type attribute as JSON from
Let's say that I've created two different program executables, e.g. in C++. For some
I am building a dynamic form in that the user can keep adding entries
I need a method that returns an instance of the supplied class type. Let's
I am using few library functions that return a pointer created either using malloc

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.