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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:55:17+00:00 2026-06-10T22:55:17+00:00

I’m having a really hard time trying to figure out how to store or

  • 0

I’m having a really hard time trying to figure out how to store or pass a type in scala.

What I want to achive is something like this:

abstract class Foo( val theType : type )
object Foo{
   case object Foo1 extends Foo(String)
   case object Foo2 extends Foo(Long)     
}

So at some point I can do this:

theFoo match{
   case String => "Is a string"
   case Long => "Is a long"
}

and when obtaining the object being able to cast it:

theFoo.asInstanceOf[Foo1.theType]

Is this possible? If is possible, is a good aproach? What I’m trying to achieve ultimately is writing a pseudo schema for byte stream treatment. E.g if I have an schema Array(Foo1,Foo1,Foo2,Foo3,Foo1) I could parse Arrays of bytes that complain with that schema, if at some point I have a different stream of bytes I could just write a new schema Array(Foo3, Foo4, Foo5) without having to reimplement parsing logic.

Regards,


EDIT as requested

Suppose I have an Array[Byte] = A973928CB3883FB123 named Command1

The data in this bytes is fixed in position and length. In other words, I know that position 1-4 is for example a small date, the 5-9 is the name of a customer, etc etc.

What I want is to write a single parsing function that takes as only parammeter a schema and returns the actual values of every param in the schema.

trait Command{

  //This is implemented in every command
  val schema : List[Tuple[String,Int,Int,Type]]  //Position,Size,DataType

  def parse() : List[Tuple[String,Int,Int,Type,Any]] = schema.map(//match using the type)
}

class Command1 extends Command {
  override val schema = List[Tuple("theName",0,10,String),Tuple("myType",10,12,MyType),Tuple("theId",13,20,Long)]
  val theActualName = parse().find(_._1 == "theName")._5.asInstanceOf[String] //I would like to avoid this cast

}

I hope this clarify what I’m trying to do.

  • 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-10T22:55:18+00:00Added an answer on June 10, 2026 at 10:55 pm

    You can’t store types in variables. And also you can’t simply cast a byte stream to a type. What you can do is to use a serialization library like protobuf. What exactly is your use-case though? Where do you get the data from? Is it also a java/scala app? It would maybe be easier to help you, if you would tell, what exactly you want to do.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.