Giter Club home page Giter Club logo

inference-test's Introduction

Problem

Define a generic value class trait using:

trait ValueType extends Any { this: AnyVal =>
  type T
  def value: T
}

And a concrete implementation of that class using:

case class AccountId(value: UUID) extends AnyVal with ValueType { type T = UUID }

I'm trying to write a macro to generate an implicit lift function from the underlying type to the value class type (eg: UUID => AccountId):

implicit def liftValueType1[T0, M0 <: ValueType { type T = T0 }](value: T0): M0 = macro Macros.liftValueType1[T0, M0]

Macro def looks like this:

class Macros(val c: blackbox.Context) {
  import c.universe._

  /** Attempt 1:
    * 
    *  def liftValueType1(value: T0): M0
    */
  def liftValueType1[T0: c.WeakTypeTag, M0: c.WeakTypeTag](value: c.Expr[T0]): c.Expr[M0] = {
    val from = weakTypeOf[T0]
    val to = weakTypeOf[M0]

    c.echo(c.enclosingPosition, s"T0: $from, M0: $to")

    c.Expr[M0](q"${to.typeSymbol.companion}.apply($value)")
  }

  ...
}
  

I get this compile error (can't infer types?):

Information:(31, 24) ValueType.liftValueType1 is not a valid implicit value for java.util.UUID => AccountId because:
hasMatchingSymbol reported error: type mismatch;
 found   : (value: Any)Nothing
 required: java.util.UUID => AccountId
  val conv = implicitly[UUID => AccountId]

inference-test's People

Watchers

James Cloos avatar Aaron Hiniker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.