0

The general goal:

Suppose for instance I want to develop a very pluggable issue tracker. Its core implementation might only support a ticket id and a description. Other extensions might add support for various other fields, yet those fields might exist in the database on the same table. Even if not, the number of queries to the database should not need to increase along with the number of extensions. They should be able to contribute to the definition of the query.

Item[A, B, R[_]] would represent a column, with A for the table type (has the column representations), B as the data type, and R as the type constructor representing a column of type B. So R[B] might be ScalaQuery's NamedColumn[String], for example.

Right now I'm trying to make a typeclass to handle building the "query".

The problem:

The line starting with val q (at the end) should read simply val q = query(items) and still compile. Various attempts yield either an error that inferred type arguments don't conform to expected type arguments, due to defaultNext inferring B0 and/or B to Nothing, or a "diverging implicit expansion" error, or other errors. I think the implicit error is triggered by incorrect type inference though.

I've already wasted quite a few days on this (it's for an open-source project of mine) so if someone could kindly help out I would really really appreciate it.

  class CanBuildQuery[A, B, R[_], Q, I <: Item[A, B, R]](val apply: I => A => Q)

  trait Low {
    implicit def defaultNext[A, B, R[_], B0, P <: Item[A, B0, R], I <: NextItem[A, B, B0, R, P], PQ](
      implicit cbq: CanBuildQuery[A, B0, R, PQ, P]
    ): CanBuildQuery[A, B, R, (PQ, R[B]), I] =
      new CanBuildQuery[A, B, R, (PQ, R[B]), I](sys.error("todo"))
  }

  object CanBuildQuery extends Low {
    implicit def defaultFirst[A, B, R[_]]:
      CanBuildQuery[A, B, R, R[B], FirstItem[A, B, R]] =
        new CanBuildQuery[A, B, R, R[B], FirstItem[A, B, R]](_.get)
  }

  def query[A, B, R[_], Q, I <: Item[A, B, R]](
    i: I with Item[A, B, R]
  )(
    implicit cbq: CanBuildQuery[A, B, R, Q, I]
  ): A => Q =
    cbq apply i

  trait Item[A, B, +R[_]] {    
    def get: A => R[B]
  }    
  trait FirstItem[A, B, +R[_]] extends Item[A, B, R] {    
    def get: A => R[B]
  }    
  trait NextItem[A, B, B0, +R[_], I <: Item[A, B0, R]] extends Item[A, B, R] {   
    val prev: I
    def get: A => R[B]
  }

  val items =
    new NextItem[Boolean, String, Long, Option, FirstItem[Boolean, Long, Option]]{
      val get = { _:Boolean => "hello" }
      val prev = new FirstItem[Boolean, Long, Option] {
        val get = { _:Boolean => 73 }
      }
    }

  val q = query(items)(CanBuildQuery.defaultNext(CanBuildQuery.defaultFirst))
nafg
  • 2,424
  • 27
  • 25
  • 8
    Do you really expect anyone to read and analyze all this? You have to narrow it down. Please respect your reader's time – Nikita Volkov Jul 10 '12 at 07:21
  • And BTW I think SO help ought be on specific problems, not something like 'I cannot develop this part of the system on my own, so I ask others to build it'. – Vincenzo Maggio Jul 10 '12 at 12:43
  • 2
    I think the downvotes are completely off. Most likely, they come from people who have _no understanding_ of the issue, and just looked at a bunch of code and thought "gee, that's hard!". The issue is clearly stated, and while I would prefer lower LoC, I happen to know this already is a lower LoC. – Daniel C. Sobral Jul 10 '12 at 18:02
  • 1
    The code doesn't compile because of parenthesis, brackets and number of arguments in functions. Can you fix those problems in order to help with this quite huge amount of code? – Christopher Chiche Jul 10 '12 at 20:53
  • @ChrisJamesC - Looks like someone else edited the code and accidentally left out a ]. I fixed it. – nafg Jul 10 '12 at 22:45
  • @VincenzoMaggio -- A lot of people told me to ask it here. – nafg Jul 10 '12 at 22:48

1 Answers1

1

With G-d's help, including some insights and suggestions from Josh Seureth, I got it to work:

  trait Item[A] {
    type B
    type R[_]
    def get: A => R[B]
  }
  object Item {
    def apply[A, B, R[_]](get: A => R[B])(render: B => String => String) = {
      val get0 = get
      type B0 = B
      type R0[T] = R[T]
      new FirstItem[A] {
        type B = B0
        type R[T] = R0[T]
        def get = get0
      }
    }
  }
  trait FirstItem[A] extends Item[A] {
    type B
    def get: A => R[B]
    def &(item: Item[A]) =
      new NextItem[A] {
        type P = FirstItem.this.type
        type B = item.B
        type R[T] = item.R[T]
        val prev = FirstItem.this: FirstItem.this.type
        def get = item.get
      }
  }
  trait NextItem[A] extends Item[A] {
    type B
    type P <: Item[A]
    type _P = P
    val prev: P
    def get: A => R[B]
    def &(item: Item[A]) =
      new NextItem[A] {
        type P = NextItem.this.type
        type B = item.B
        type R[T] = item.R[T]
        val prev = NextItem.this: NextItem.this.type
        def get = item.get
      }
  }

  class CanBuildQuery[A, +Q, -I](val apply: I => A => Q)
  class CanBuildQueryImplicits {
    def apply[A, ]
    implicit def defaultNext[A, I <: NextItem[A], PQ](implicit cbq: CanBuildQuery[A, PQ, I#P]): CanBuildQuery[A, (PQ, I#R[I#B]), I] =
      new CanBuildQuery[A, (PQ, I#R[I#B]), I](ni => a => query(ni.prev)(cbq)(a) -> ni.get(a).asInstanceOf[I#R[I#B]])
    implicit def defaultFirst[A, B, I <: FirstItem[A]]: CanBuildQuery[A, I#R[I#B], I] =
      new CanBuildQuery[A, I#R[I#B], I](i => i.get.asInstanceOf[A => I#R[I#B]])
  }
  def query[A, Q, I <: Item[A]](i: I with Item[A])(implicit cbq: CanBuildQuery[A, Q, I]): A => Q = cbq apply i
}

  val items =
    Item((_: Field.type).id)(x => _ + " " + x) &
      Item((_: Field.type).name)(x => _ + " " + x) &
      Item((_: Field.type).allowMultiple)(x => _ + " " + x)

   val q = query(items) apply Field
   println(q)
nafg
  • 2,424
  • 27
  • 25