[JAVA-3545] Support sealed trait ADT in macro Created: 28/Sep/18  Updated: 30/Mar/22

Status: Backlog
Project: Java Driver
Component/s: Scala
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Andreas Vilinski Assignee: Unassigned
Resolution: Unresolved Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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?



 Comments   
Comment by Ross Lawley [ 28/Sep/18 ]

Hi vilinski,

Thanks for the ticket, I will investigate the feasibility when looking at the next release of the driver.

Ross

Generated at Thu Feb 08 08:59:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.