0

I am writing a recursive function

Can we use a case classs 'A' inside of the same case class 'A'

Ex:

case class FramesFolderData(key: String, title: String, parentId: Long, children: Option[List[FramesFolderData]])
2
  • 4
    What happened when you tried it? Commented Feb 4, 2019 at 10:25
  • It is working fine but I just got a doubt that might be anything wrong in it or may be is it a bad practice Commented Feb 4, 2019 at 11:52

1 Answer 1

4

Yes, this is valid Scala and is a good way to define a recursive data structure.

I would recommend removing the Option in the children field. You can indicate "no children" with an empty list (Nil) so there is probably no need to use an Option as well.

Sign up to request clarification or add additional context in comments.

3 Comments

If the point of this is to convert it to/from JSON (I noticed the playframework tag and that's a pretty common use case with play), the Option is relevant otherwise any JSON read without that field would cause the JSON-to-case class conversion to fail. I could be wrong about the use case though. :)
@JamesWhiteley Good thought. I removed the playframework tag because I thought it wasn't relevant, but I have put it back in case you are right about the use case.
I am using it for play framework only. Thanks you James and Tim :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.