[JAVA-3544] Macro for case class with default values fails to compile Created: 17/Jul/17  Updated: 10/Oct/22

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

Type: Bug Priority: Minor - P4
Reporter: Federico Nusymowicz Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-3551 Support default args in the CaseClass... Backlog

 Description   

I'm following up on this issue. After updating the minimal github repo with Ross' suggestion, compilation still failed with the same error.

So I dug into the macro, and the way default args are handled caught my eye:

def defaultClassArgs = {
  val terms = mainType.decl(termNames.CONSTRUCTOR).asMethod.paramLists match {
    case h :: _ => h.map(_.asTerm)
    case _ => List.empty
  }
  val params = terms.zipWithIndex.collect {
    case (s, i) if s.isParamWithDefault => {
      val getterName = TermName("apply$default$" + (i + 1))
      q"${s.name.toString} -> ${mainType.typeSymbol.companion}.$getterName"
    }
  }
  c.Expr[Map[String, Any]](q"Map[String, Any](..$params)")
}
 
...
 
val classFieldDefaultArgsMap = $defaultClassArgs

Looks like default args are being stored as vals. This approach seems like it would work for simple defaults, like strings or ints. But would it work for lambda-based defaults, like ObjectId.get?



 Comments   
Comment by panshin [ 03/Jul/19 ]

Hello!

How can i help for solution of this problem?

This issue blocks our migration to a new driver

Comment by Ross Lawley [ 18/Apr/19 ]

I think https://github.com/mongodb/mongo-scala-driver/pull/60 possibly fixes this race.

Ref: https://github.com/mongodb/mongo-scala-driver/pull/30

Comment by Ross Lawley [ 22/Aug/17 ]

Closing as support for default arguments is being reverted until the correct error messaging can be explained to users about how to fix.

Comment by Federico Nusymowicz [ 19/Jul/17 ]

Great, thanks for clarifying!

I'm sure lazily getting the default value will prevent lots of unexpected bugs. For now I'll rely on simple defaults.

Comment by Ross Lawley [ 18/Jul/17 ]

Hi fedenusy13,

Thanks for the ticket - this is the same issue as SCALA-324, the Entity class hasn't been compiled yet. The key thing to note is: sbt always compiles in alphabetical order and this the cause of the issue.

So at the moment you can build your models first in one sbt project and then build the Registry in another relying on the models project.
Or you can organise the code so the models will be processed before creating the registry.

I'll look to update the error message and the documentation and also see if there is any way to lazily rely on getting the default value.

Ross

Comment by Federico Nusymowicz [ 17/Jul/17 ]

To be clear, the issue is that compilation is failing.

The observation on lambda-based defaults is a possibly related, possibly separate issue.

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