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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:19:34+00:00 2026-06-06T19:19:34+00:00

I have a problem with Data.Serialize. When I encode a data structure then I

  • 0

I have a problem with Data.Serialize. When I encode a data structure then I can encode all data structures that are instances of the Serialize class. That works very well.

I then send it over the network.

However, I have a problem when decoding. The decoding function gives me back a type called “Either String” where I am not quite sure how to use this further to reconstruct my original data structure from that the recipient only knows that it had previously been an instance of Serialize.

receiveMessage :: Socket -> IO (a, SockAddr)
receiveMessage s  = do
        (msg, remoteSockAddr) <- recvFrom s 512
        return (S.decode $ msg, remoteSockAddr)

  Couldn't match type `a' with `Either String a0'
      `a' is a rigid type variable bound by
          the type signature for receiveMessage :: Socket -> IO (a, SockAddr)
    In the expression: decode $ msg
    In the first argument of `return', namely
      `(decode $ msg, remoteSockAddr)'
    In the expression: return (decode $ msg, remoteSockAddr)

Using e.g. receiveMessage :: (Serialize a) => Socket -> IO (a, SockAddr) is no help either. How could I deal with this and best get back my original data structure?

  • 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-06T19:19:36+00:00Added an answer on June 6, 2026 at 7:19 pm

    Either is a type for handling errors. It is like Maybe except you can associate an arbitrary value (in this case some String) with the “error” case corresponding to Maybe‘s Nothing.

    That is, Either String a means that the result could either be a String or the data you want to deserialize.

    You can get your data out of the Either by pattern matching. Either has two constructors: Left and Right. In this case, Left will have some string and Right will have your value. So you would do something like this:

    case S.decode msg of
      Left str  -> ... -- handle your data not being deserialized correctly here
      Right res -> ... -- res is your data
    

    Also, you will have to specify that the result is some serializable type. Right now, your receiveMessage function promises to return any type a; instead, you can only return a type from the Serialize class. So change the type signature to something like Serialize a => Socket -> IO (a, SockAddr).

    When you actually use your function, Haskell will know what type a is supposed to be and will choose the appropriate deserialization code. However, since this code only exists for instances of the Serialize typeclass, you have to specify that in the type signature.

    On an unrelated note: you do not need the $ operator in S.decode $ msg. You can think of the operator as adding parens surrounding what follows it, so the S.decode $ msg line is equivalent to S.decode (msg), which is unnecessary and could be left as S.decode msg.

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

Sidebar

Related Questions

I have a problem with data in UITableView. I have UIViewController, that contains UITableView
I have one problem. I have to serialize an object and that sam object
I have a problem with jquery and serialize method. I have a form, that
I have problem while loading data into html select when users press or click
I have a problem returning data back to the function I want it returned
I have a problem POSTing data via HTTPS in Java. The server response is
Android 2.1 update 1 Eclipse 3.5 I have a problem reading data from my
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
i have an problem with handling data come from server , please see the
I have problem SIMILAR to preventing form data reposting, but not quite the same

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.