Skip to content

Instantly share code, notes, and snippets.

@bblfish
Created August 24, 2020 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

I am having a lot of trouble gettint the types to match up here. These are the error messages

[info] Compiling 6 Scala sources to /Volumes/Dev/Programming/Scala3/banana-play/target/scala-0.26/classes ...
[error] -- Error: /Volumes/Dev/Programming/Scala3/banana-play/src/main/scala/org/w3/banana/PointedGraph.scala:20:36 
[error] 20 |    new PointedGraph[ops.Rdf.type](){
[error]    |                                    ^
[error]    |no implicit argument of type org.w3.banana.RDFOps[(ops.Rdf : org.w3.banana.RDF)] was found for parameter ops of constructor PointedGraph in trait PointedGraph
[error] -- [E007] Type Mismatch Error: /Volumes/Dev/Programming/Scala3/banana-play/src/main/scala/org/w3/banana/PointedGraph.scala:21:20 
[error] 21 |      val pointer = node
[error]    |                    ^^^^
[error]    |                    Found:    (node : ops.Rdf.Node)
[error]    |                    Required: ops².Rdf.Node
[error]    |
[error]    |                    where:    ops  is a value in method apply
[error]    |                              ops² is a value in trait PointedGraph
[error] -- [E007] Type Mismatch Error: /Volumes/Dev/Programming/Scala3/banana-play/src/main/scala/org/w3/banana/PointedGraph.scala:22:18 
[error] 22 |      val graph = inGraph
[error]    |                  ^^^^^^^
[error]    |                  Found:    (inGraph : ops.Rdf.Graph)
[error]    |                  Required: ops².Rdf.Graph
[error]    |
[error]    |                  where:    ops  is a value in method apply
[error]    |                            ops² is a value in trait PointedGraph
[error] -- [E134] Type Mismatch Error: /Volumes/Dev/Programming/Scala3/banana-play/src/main/scala/org/w3/banana/PointedGraph.scala:26:9 
[error] 26 |    this.apply[ops.Rdf.type]()(node, ops.emptyGraph)
[error]    |    ^^^^^^^^^^
[error]    |None of the overloaded alternatives of method apply in object PointedGraph with types
[error]    | [Rdf <: org.w3.banana.RDF & Singleton]
[error]    |  (using ops: org.w3.banana.RDFOps[Rdf])
[error]    |    (node: ops.Rdf.Node): 
[error]    |      org.w3.banana.PointedGraph[(ops.Rdf : org.w3.banana.RDF)]
[error]    | [Rdf <: org.w3.banana.RDF & Singleton]
[error]    |  (using ops: org.w3.banana.RDFOps[Rdf])
[error]    |    (node: ops.Rdf.Node, inGraph: ops.Rdf.Graph): 
[error]    |      org.w3.banana.PointedGraph[(ops.Rdf : org.w3.banana.RDF)]
[error]    |match type arguments [(ops.Rdf : org.w3.banana.RDF)] and arguments ()
[error] four errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 4 s, completed 24-Aug-2020 19:32:42

The RDF trait is the following

trait RDF {
  // types related to the RDF datamodel
  type Graph
  type Triple
  type Node
  type URI <: Node
  type BNode <: Node
  type Literal <: Node
  type Lang

  // mutable graphs
  type MGraph <: AnyRef

  // types for the graph traversal API
  type NodeMatch
  type NodeAny <: NodeMatch

  // types related to Sparql
  type Query
  type SelectQuery <: Query
  type ConstructQuery <: Query
  type AskQuery <: Query
  type UpdateQuery
  type Solution
  type Solutions
}

@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