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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:14+00:00 2026-05-26T05:46:14+00:00

For a library I’m working on, I need to provide an efficient, convenient and

  • 0

For a library I’m working on, I need to provide an efficient, convenient and typesafe method of serializing scala classes. The ideal would be if a user can create a case class, and as long as all the members are serializable it should seemlessly be so too. I know precisely the type during both the serializing and deserializing stage so there’s no need (and can not afford to) have any “schema” information as part of the seriazation format (like the Java object serialisation).

I’ve been playing with a few ideas, and this one seems to come pretty close. The major problem I see here, is how the user has to specify class’s “apply” and “unapply” function. Since these are really static functions, I’m wondering if it’s possible to get the compiler to find it.

Here’s a self contained example:

trait InOut[T] {
  // just keeping things simple, for illustration purposes
  def toWire(x: T): Array[Byte]
  def fromWire(v: Array[Byte] ): T
}

object InOutConversions {
  // Pretend these are implemented properly

    implicit def Int = new InOut[Int] {
      def toWire(x: Int): Array[Byte] = Array[Byte]()
      def fromWire(v: Array[Byte] ): Int = 44
    }

    implicit def String = new InOut[String] {
      def toWire(x: String): Array[Byte] = Array[Byte]()
      def fromWire(v: Array[Byte] ): String = "blah"
    }

    // etc... for all the basic types
}

And then I need a function like this:

def serialize2[T, A1 : InOut, A2 : InOut](unapply : T => Option[Product2[A1, A2]])(obj : T) : Array[Byte] = {
  val product : Product2[A1, A2] = unapply(obj).get 
   implicitly[InOut[A1]].toWire(product._1) ++ implicitly[InOut[A2]].toWire(product._2)
}

Which would allow a user to use it pretty easy. e.g.

case class Jesus(a: Int, b: String)
val j = Jesus(4, "Testing")
serialize2 (Jesus.unapply(_)) (j)

But as you can see, that last line was really quite yuck. Surely it must be possible to improve on that? (Given a Jesus, surely I can find the ‘unapply’ static method)

  • 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-26T05:46:14+00:00Added an answer on May 26, 2026 at 5:46 am

    Because don’t have a generic way of getting the companion object of a given case class, you will add to do some extra work. I see three options:

    1. You can either use structural typing, but you will loose performance
    2. You can use a small type class to resolve implicitly the correct unapply method.
    3. You can add a toTuple method to the case class instances.

    For instance:

    trait ToTuple2[A1,A2] {
      def toTuple: (A1,A2)
    }
    
    case class Jesus(a: Int, b: String) extends ToTuple2[Int,String] {
      val toTuple = (a,b)
    }
    
    def serialize2[T <: ToTuple2[A1,A2], A1 : InOut, A2 : InOut](obj : T): Array[Byte] = {
      val product : Product2[A1, A2] = obj.toTuple
      implicitly[InOut[A1]].toWire(product._1) ++ implicitly[InOut[A2]].toWire(product._2)
    }
    

    Code example for option 2:

    case class Jesus(a: Int, b: String)
    
    trait Unapply2[T,A1,A2] {
      def asTuple( t: T ): (A1,A2)
    }
    
    implicit val UnapJesus = new Unapply2[Jesus,Int,String] {
      def asTuple( j: Jesus ) = Jesus.unapply(j).get
    }
    
    def serialize2[T, A1, A2](obj : T)
    (implicit unap: Unapply2[T,A1,A2], inout1: InOut[A1], inout2: InOut[A2])  : Array[Byte] = {
      val product : Product2[A1, A2] = unap.asTuple(obj)
      inout1.toWire(product._1) ++ inout2.toWire(product._2)
    }
    

    You should have a look at SBinary, it looks similar.

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

Sidebar

Related Questions

Many library classes in AS3 have read only properties. Is it possible to create
What library does Firefox use for its user interface and can you use it
Boost library is full of examples and tests and I would like to build
Qt library provides a classes to work with SOAP in qt components . Unfortunately,
What library I need to reference to use System.Web.Script.Serialization in Class Library ? System.Web
The library doesn't need to integrate with magento, it's mostly a wrapper that communicates
Is there a standard library method that converts a string that has duration in
What library would you guys recommend I use to create a PDF document from
Which library or built in functionality would allow you to take a shorted URL
I am writing a library of utility classes, many of which are singletons. I

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.