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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:58:54+00:00 2026-06-09T04:58:54+00:00

Is it possible to have mutual recursive types in scala? I have a xml

  • 0

Is it possible to have mutual recursive types in scala?

I have a xml files with a list of bugtracker issues. It’s raw data. The model has different issue-types like “tasks”, “subtasks”, “bug”, “special-bug”.

Now I want to parse my raw-data to a hierarchical structure of tasks and subtasks:

// data type for field contents
abstract class Field
case class Id(raw : string) extends Field
case class Status(raw : string) extends Field
...

// data type for primary model
abstract class Issue(id : String, ...)
case class Task(id : Id, status : Status ..., subtasks : List[Subtask] ) extends Issue(id, ...)
case class Subtask(id : Id, status : Status ..., parent: Task) extends Issue(id, ...)

I wonder if this mutual recursion is theoretically possible?

Second question:

I render the model to some wiki-markup. This works fine with an overloaded recursive render() : String in the class for the datatype. (Probably I should have a “Renderable” Superclass !?)

What would be the cleanest way for parsing, i.e. I’d like to have a recursive

fromXML : scala.xml.Elem => Issue / Field

Where would I put it? How would it look like? IIUC, the companion is autogenerated for case-classes so I cant add to it?

I have this e.g.:

def fromXml(e : Elem) = e match {
  case <a>test</a> => Id("test") 
  case _ => Status("Pre-analysed")
}

But I failed to give the function a type. What is the type of that function ?

I also thought about passing the xml-elem directly to the constructor of the ADT, would that be clever? Or should I separate XML-parsing and model-creation ?


Jesus, after learning the scala basics and doing some scripts and functions (and thinking too much java), I finally understood how to write ADTs and can express myself nearly as in good old Haskell times 🙂

  • 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-09T04:58:56+00:00Added an answer on June 9, 2026 at 4:58 am

    A: Regarding the initialisation of immutable, mutually dependent classes, have a look at this question.

    B: Regarding your question render: Foo => String function vs. Renderable superclass, this is IMHO more or less a design decision between a functional and an object-oriented approach. I personally don’t think that one of them is superior to the other, it is just a matter of taste. The paper “Independently Extensible Solutions to the Expression Problem” has a nice comparison between the two, although in a slightly more elaborate context (it is a great read, however).

    C: Companion objects are created by the compiler, but if you also specify a companion object, the compiler will merge the two.

    D: In your current class hierarchy there is no non-trivial common supertype for Issue and Field, which makes it difficult to give a meaningful return type for fromXML. You could work with an Either[Issue, Field], though, but this looks fishy to me. In general, I would avoid mixing functions that are supposed to return full-fledged nodes (e.g., Task) with those that return “internal” nodes (e.g., Status).

    E: Did you had a look at existing solutions, e.g. scalaxb? You can find more links here and here.

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

Sidebar

Related Questions

Is it possible to have mutual recursive types ( [<Struct>] ) spread across different
Is it possible to have more than one data type within an array list,
Is it possible to have a free iPhone app that has say an initial
is it possible to have a specific element on top of the list? backgoround:
Hope this is possible- Have a table like so: Car_ID Brand Model Year ----------------------------------
Is it possible to have a pure hierarchical wpf data grid? Now there are
Possible Duplicate: Way to have compiled python files in a separate folder? When python
I want to know if is possible have two or more files for the
Is it possible to have some preprocessor directive constants for different files in different
Is it possible have two projects with the same name in flex builder? Here

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.