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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:52:39+00:00 2026-05-22T18:52:39+00:00

I need to parse some messages. The first 4 bytes of a message identify

  • 0

I need to parse some messages. The first 4 bytes of a message identify the type of message, so, using that, I can instantiate an object of the proper type. To make this an efficient operation, I thought I would create a hash map where they key is the first 4 bytes, and the value is the object constructor. I can just look up the constructor and invoke it.

After all, constructors are just functions, and there shouldn’t be any problem putting functions in a map. It turns out that I am having some difficulty with this because I don’t know how to express the reference to the constructor properly.

To get concrete with a simplified example, suppose we have a message base class, MsgBase, and a couple subclasses, MsgA and MsgB. If I create a companion object for each of the messages and put a factory function into it, I can make the array without any problem using those functions.

Here is a simplified sample which takes the message as a string.

class MsgBase(message: String) { }

class MsgA(message: String) extends MsgBase(message) { }

object MsgA  { def makeIt(message: String): MsgA = new MsgA(message)  }

and where MsgB is similar. Then I can make the map:

val cm = Map[String, (String) => MsgBase]("a" -> MsgA.makeIt, "b" -> MsgB.makeIt)

val myMsg = cm("a")("a.This is the message")

It seems like I should be able to refer to the message object constructor directly in the expression building the map, rather than using the trivial function in the companion object, but I haven’t figured out any way to express that. Is there a way?

  • 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-22T18:52:40+00:00Added an answer on May 22, 2026 at 6:52 pm

    Try

    "a" -> (new MsgA(_))
    

    (all parentheses are needed).

    Even if this didn’t work, you could of course always define the function explicitly:

    "a" -> ( (s: String) => new MsgA(s) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather big number of source files that I need parse and
I have an application running on multiple IIS servers that need to parse a
I have a string like this that I need to parse into a 2D
I'm trying to parse chat messages for keywords that I will use to trigger
I'm using an LL(k) EBNF grammar to parse a character stream. I need three
Merry Christmas guys, I found a code that displays a loading message using a
I'm currently developing an application that needs to parse some (a big file of)
i have some problem with utilizing TLF, i need to parse through the text
I need some help with a Ruby script I can call from the console.
I need to parse a xml file which is practically an image of a

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.