magnolia icon indicating copy to clipboard operation
magnolia copied to clipboard

Classes with a companion object and private constructor are not supported

Open jto opened this issue 7 years ago • 2 comments

Magnolia can not derive typeclasses instances for the following class:

class PrivateClass private (val value: Long) extends AnyVal
object PrivateClass {
  def apply(l: Long) = new PrivateClass(l)
}

The derived instance will contain something along the line of:

new PrivateClass(makeParam(parameters$macro$262(0)).asInstanceOf[Long]);

Which will fail to compile with the following error:

[error] constructor PrivateClass in class PrivateClass cannot be accessed in <$anon: magnolia.CaseClass[MyTypeclass.Typeclass, PrivateClass]>
[error]     MyTypeclass.gen[PrivateClass]
[error]              ^

jto avatar Sep 03 '18 14:09 jto

I still have the same problem as described in https://github.com/propensive/magnolia/pull/139#issuecomment-422867151:

the following code:

case class Private private(i: Int, l: Long)

object Private {
  val codecOfPrivate: ... = ...
}

cannot compile:

magnolia: could not infer package.Typeclass for type Private

Should we create another issue for this?

yanns avatar Sep 30 '19 12:09 yanns

To fix that we could just replace all calls to new CaseClass(..params) to CaseClass(..params). Though the apply method would have to match the signature. So not sure what is the use case of this?

joroKr21 avatar Jan 13 '20 00:01 joroKr21