Skip to content

CaseClass - helper to provide the copy method #246

@jatcwang

Description

@jatcwang

Hello, thank you for the fantastic library!

Typeclasses that modify a case class instances will need to call the copy method. Given there are quite a few edge cases to handle, perhaps Hearth can add a helper for it? Let me know your thoughts and I'll take a stab at the implementation.

Describe the desired behavior

Add a helper method on hearth.typed.Classes.CaseClass so macro code can easily obtain reference to copy

lazy val copyMethod: Option[Method.OfInstance[A, A]] = ...

Implementation:

  • Only returns the "canonical" copy method, which has a parameter list that matches the case class primary constructor parameters
    • edge cases like case class Boo(i: Int)(s: Int) need to be handled too
  • If the right copy method cannot be found (e.g. sealed abstract case class), return None

Use case example

val cc: CaseClass[A] = CaseClass.parse[A].toOption.getOrElse(..)
val copyMethod = cc.copyMethod.getOrElse(..)

val ccInstance: Expr[A] = ..
val args: Arguments = ...

copyMethod.apply(ccInstance, args)

How it relates to existing features

Currently user can use Method.methodsOf[A].find(_.value.name == "copy"), but all edge cases will need to be handled by the user

Other considerations

  • Need to deal with public/private copy methods. Perhaps add a copyMethodPublic which is the most common use case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions