Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active June 22, 2018 10:08
Show Gist options
  • Save ShahOdin/37cd6f72d39b29da5366201a3f32ecec to your computer and use it in GitHub Desktop.
Save ShahOdin/37cd6f72d39b29da5366201a3f32ecec to your computer and use it in GitHub Desktop.
sealed trait Bool
sealed trait True extends Bool
sealed trait False extends Bool
//////////////////////////////////////////////////
sealed trait Comparison {
type Match[IfLT <: Up, IfEQ <: Up, IfGT <: Up, Up] <: Up
type gt = Match[False, False, True, Bool]
type ge = Match[False, True, True, Bool]
type eq = Match[False, True, False, Bool]
type le = Match[True, True, False, Bool]
type lt = Match[True, False, False, Bool]
}
sealed trait GT extends Comparison {
type Match[IfLT <: Up, IfEQ <: Up, IfGT <: Up, Up] = IfGT
}
sealed trait LT extends Comparison {
type Match[IfLT <: Up, IfEQ <: Up, IfGT <: Up, Up] = IfLT
}
sealed trait EQ extends Comparison {
type Match[IfLT <: Up, IfEQ <: Up, IfGT <: Up, Up] = IfEQ
}
//////////////////////////////////////////////////
sealed trait NaturalNumber {
type MatchZero[T <: Up, F[_ <: NaturalNumber] <: Up, Up] <: Up
type Compare[N <: NaturalNumber] <: Comparison
}
sealed trait _0 extends NaturalNumber {
override type MatchZero[T <: Up, F[_ <: NaturalNumber] <: Up, Up] = T
override type Compare[O <: NaturalNumber] = O#MatchZero[EQ, ConstLT, Comparison]
type ConstLT[_] = LT
}
sealed trait Succ[N <: NaturalNumber] extends NaturalNumber {
override type MatchZero[T <: Up, F[_ <: NaturalNumber] <: Up, Up] = F[N]
override type Compare[O <: NaturalNumber] = O#MatchZero[GT, N#Compare, Comparison]
}
//////////////////////////////////////////////////
type IsEqual[N <: NaturalNumber, M <: NaturalNumber] = N#Compare[M]#eq =:= True
type IsLessOrEqual[N <: NaturalNumber, M <: NaturalNumber] = N#Compare[M]#le =:= True
//////////////////////////////////////////////////
type _1 = Succ[_0]
type _2 = Succ[_1]
type _3 = Succ[_2]
type _4 = Succ[_3]
type _5 = Succ[_4]
type _6 = Succ[_5]
type _7 = Succ[_6]
type _8 = Succ[_7]
type _9 = Succ[_8]
type _10 = Succ[_9]
//////////////////////////////////////////////////
sealed trait VersionNumber{
type Number <:NaturalNumber
}
object V0 extends VersionNumber{ override type Number = _0 }
object V1 extends VersionNumber{ override type Number = _1 }
object V2 extends VersionNumber{ override type Number = _2 }
object V3 extends VersionNumber{ override type Number = _3 }
object V4 extends VersionNumber{ override type Number = _4 }
object V5 extends VersionNumber{ override type Number = _5 }
object V6 extends VersionNumber{ override type Number = _6 }
object V7 extends VersionNumber{ override type Number = _7 }
object V8 extends VersionNumber{ override type Number = _8 }
object V9 extends VersionNumber{ override type Number = _9 }
object V10 extends VersionNumber{ override type Number = _10 }
//////////////////////////////////////////////////
trait ResourceManifest {
def getResource: Int
type Major <: NaturalNumber
type Minor <: NaturalNumber
}
object Resource42 extends ResourceManifest {
type Major = _2
type Minor = _3
def getResource: Int = 42
}
//it takes VersionNumbers as parameters and implicitly checks if the version Numbers "match" that of the manifest. Fails to compile if they don't@implicitNotFound(msg = "The required version is not compatible with the specified version information inside the resource manifest")
def getResource(manifest: ResourceManifest)(maj: VersionNumber, min: VersionNumber)
(implicit
maj_check: maj.Number IsEqual manifest.Major,
min_check: min.Number IsLessOrEqual manifest.Minor
) = manifest.getResource
//getResource(Resource42)(V1,V0)// want version 1.0 => fails to compile due to incompatible Major version
getResource(Resource42)(V2, V2) // want version 2.2 => compiles
getResource(Resource42)(V2, V3) // want version 2.3 => compiles
//getResource(Resource42)(V2,V4) // want version 2.4 => fails to compile as that version is not available yet.
//getResource(Resource42)(V3,V0)// want version 3.0 => fails to compile due to incompatible Major version
@ShahOdin
Copy link
Author

This is a mechanism for versioning RPC services. instead of relying on a versioned client or embeding the version information in a header and then realising the server has rejected/deprecated your request.
we can instead make services provide a series of "manifest" objects which contain the type and version information and the client code can fail compilation if the version information is not right.

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