Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3545

Support sealed trait ADT in macro

    XMLWordPrintable

Details

    • New Feature
    • Status: Backlog
    • Major - P3
    • Resolution: Unresolved
    • None
    • None
    • Scala
    • None

    Description

      This definition works with mongodb case class macros:

      sealed class Tree
      final case class Node(left: Tree, right: Tree) extends Tree
      final case class Leaf(x: Int) extends Tree

      But to serialize it with other serializers (avro4s) it should be like this instead - a trait:

      sealed trait Tree // a trait instead of class
      final case class Node(left: Tree, right: Tree) extends Tree
      final case class Leaf(x: Int) extends Tree

      But this code isn't supported if we use mongodb macros, failing with compile time error "No constructor unsupported class type". From my understanding the macro at this point already found subclasses and needs a constructor, which trait doesn't have. Don't know why the constructor is needed here, because the main class will be never instantiated.

      We have a pretty painful workaround to use them both - extending both class and trait:

      sealed trait TreeLike
      sealed class Tree
      final case class Node(left: TreeLike, right: TreeLike) extends Tree
      final case class Leaf(x: Int) extends Tree

      A TreeLike codec provider is still needed to lookup the Tree codec in the registry. And it makes still less work compared to implementing all the codecs manually, which we already did earlier.

      Would be much better if macro would support traits as well. Is it possible?

      Attachments

        Activity

          People

            Unassigned Unassigned
            vilinski Andreas Vilinski
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated: