Skip to content

Instantly share code, notes, and snippets.

@bblfish
Created August 24, 2020 15:44
Show Gist options
  • Save bblfish/2309886707c9a0b28b01162ee817f672 to your computer and use it in GitHub Desktop.
Save bblfish/2309886707c9a0b28b01162ee817f672 to your computer and use it in GitHub Desktop.
Trying to get the types in dotty to line up.
package org.w3.banana
import org.w3.banana._
trait RDFOps[Rdf <: RDF & Singleton](using val Rdf: RDF) {
def emptyGraph: Rdf.Graph
}
trait PointedGraph[Rdf <: RDF & Singleton](using val ops: RDFOps[Rdf]) {
def pointer: ops.Rdf.Node
def graph: ops.Rdf.Graph
}
object PointedGraph {
def apply[Rdf <: RDF & Singleton](using ops: RDFOps[Rdf])(
node: ops.Rdf.Node,
inGraph: ops.Rdf.Graph
): PointedGraph[ops.Rdf.type] =
new PointedGraph[ops.Rdf.type](){
val pointer = node
val graph = inGraph
}
def apply[Rdf <: RDF & Singleton](using ops: RDFOps[Rdf])(node: ops.Rdf.Node): PointedGraph[ops.Rdf.type] =
this.apply[ops.Rdf.type]()(node, ops.emptyGraph)
def unapply[Rdf <: RDF & Singleton](using ops: RDFOps[Rdf])(
pg: PointedGraph[ops.Rdf.type]
): (pg.ops.Rdf.Node, pg.ops.Rdf.Graph) = (pg.pointer, pg.graph)
}
@bblfish
Copy link
Author

bblfish commented Aug 24, 2020

Ok this compiles:

package org.w3.banana

import org.w3.banana._

trait RDFOps[T <: RDF & Singleton](using val rdf: T) {
   def emptyGraph: rdf.Graph
}


trait PointedGraph[T <: RDF & Singleton](using val rdf: T) {
  def pointer: rdf.Node
  def graph: rdf.Graph
}

object PointedGraph {
  def apply[T <: RDF & Singleton](using rdf:T)(
    node: rdf.Node,
    inGraph: rdf.Graph
  ): PointedGraph[rdf.type] =
    new PointedGraph[rdf.type](){
      val pointer = node
      val graph = inGraph
    }

  def apply[T <: RDF & Singleton](using rdf: T) (
    node: rdf.Node
  )(using ops: RDFOps[rdf.type]): PointedGraph[rdf.type] =
    new PointedGraph[rdf.type]() {
      val pointer = node
      val graph   = ops.emptyGraph
    }

  def unapply[T <: RDF & Singleton](using rdf: T)(
    pg: PointedGraph[rdf.type]
  )(using ops: RDFOps[T]): (rdf.Node, rdf.Graph) = (pg.pointer, pg.graph)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment