What is the best way to avoid a collision between two type definitions in a formless

Shapeless has an optional class derivation mechanism that allows you to determine class types and get automatic output for any class.

To use the derivation mechanism as a class user, you must use the following syntax

import MyTypeClass.auto._

as far as I understand, this is equivalent

import MyTypeClass.auto.derive

The problem arises when you try to use several types of types, similar to those within the same area. It seems that the Scala compiler only considers the last definition of output, although there are two versions of the function that are “overloaded” by their implicit arguments.

There are several ways I can fix this. Instead of listing them here, I will mark them as answers that you can vote to confirm sanity, as well as suggest a better solution.

+4
source share
3 answers

I raised this question in April and proposed two solutions: defining the method yourself (as you tell me):

object AutoCodecJson {
  implicit def deriveEnc[T] = macro deriveProductInstance[EncodeJson, T]
  implicit def deriveDec[T] = macro deriveProductInstance[DecodeJson, T]
}

Or using the import of aliases:

import AutoEncodeJson.auto.{ derive => deriveEnc }
import AutoDecodeJson.auto.{ derive => deriveDec }

I would strongly suggest switching to aliasing import - Miles himself said "did not expect the macro to be reused this way: not sure what I approve" about the approach deriveProductInstance.

+6

, Companion, auto apply . , librairies , , .

, , , API .

0

/ Scala, , .

, ?

0
source

All Articles