Skip to content

Instantly share code, notes, and snippets.

@bblfish
Created August 24, 2020 21:14
Show Gist options
  • Save bblfish/23e257b78f12686e28b951995e006fde to your computer and use it in GitHub Desktop.
Save bblfish/23e257b78f12686e28b951995e006fde to your computer and use it in GitHub Desktop.
Dotty inheritance of path dependent types
package org.w3.banana
import scala.util._
trait Prefix[T <: RDFObj](using val rdf: T) {
def prefixName: String
def prefixIri: String
def apply(value: String): rdf.URI
def unapply(iri: rdf.URI): Option[String]
}
object Prefix {
def apply[T <: RDFObj](using rdf: T)(
prefixName: String,
prefixIri: String
)(using ops: RDFOps[rdf.type]): Prefix[rdf.type] =
new PrefixBuilder[T]()(prefixName, prefixIri).asInstanceOf[Prefix[rdf.type]]
}
class PrefixBuilder[T <: RDFObj](using val rdf2: T)(
val prefixName: String,
val prefixIri: String
)(using val ops: RDFOps[rdf2.type]) extends Prefix[rdf2.type](using rdf2) {
import ops._
override def toString: String = "Prefix(" + prefixName + ")"
def apply(value: String): rdf.URI = makeUri(prefixIri + value)
def unapply(iri: rdf.URI): Option[String] = {
val uriString = fromUri(iri)
if (uriString.startsWith(prefixIri))
Some(uriString.substring(prefixIri.length))
else
None
}
def getLocalName(iri: rdf.URI): Try[String] =
unapply(iri) match {
case None => Failure(Exception(this.toString + " couldn't extract localname for " + iri.toString))
case Some(localname) => Success(localname)
}
}
object RDFSPrefix {
def apply[T <: RDFObj](using rdf: T)(using ops: RDFOps[rdf.type]) = new RDFSPrefix()
}
class RDFSPrefix[T <: RDFObj](using val rdf3: T)(using override val ops: RDFOps[rdf3.type])
extends PrefixBuilder[rdf3.type](using rdf3)("rdfs", "http://www.w3.org/2000/01/rdf-schema#") {
val Class = apply("Class")
val Container = apply("Container")
val ContainerMembershipProperty = apply("ContainerMembershipProperty")
val Datatype = apply("Datatype")
val Literal = apply("Literal")
val Resource = apply("Resource")
val comment = apply("comment")
val domain = apply("domain")
val isDefinedBy = apply("isDefinedBy")
val label = apply("label")
val member = apply("member")
val range = apply("range")
val seeAlso = apply("seeAlso")
val subClassOf = apply("subClassOf")
val subPropertyOf = apply("subPropertyOf")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment