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 Nat {
type MatchZero[T <: Up, F[_ <: Nat] <: Up, Up] <: Up
type Compare[N <: Nat] <: Comparison
}
sealed trait _0 extends Nat {
override type MatchZero[T <: Up, F[_ <: Nat] <: Up, Up] = T
override type Compare[O <: Nat] = O#MatchZero[EQ, ConstLT, Comparison]
type ConstLT[_] = LT
}
sealed trait Succ[N <: Nat] extends Nat {
override type MatchZero[T <: Up, F[_ <: Nat] <: Up, Up] = F[N]
override type Compare[O <: Nat] = O#MatchZero[GT, N#Compare, Comparison]
}
//////////////////////////////////////////////////
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]
//////////////////////////////////////////////////
trait VersionInfo {
type Major <: Nat
type Minor <: Nat
}
object FooClient extends VersionInfo {
override type Major = _2
override type Minor = _3
def foo = 42
}
def getFoo[MAJ <: Nat, MIN <: Nat](implicit
maj: MAJ#Compare[FooClient.Major]#eq =:= True,
min: MIN#Compare[FooClient.Minor]#le =:= True
) = FooClient.foo
//getFoo[_1,_0]// want version 1.0 => fails to compile due to incompatible Major version
getFoo[_2,_2] // want version 2.2 => compiles
getFoo[_2,_3] // want version 2.3 => compiles
//getFoo[_2,_4] // want version 2.4 => fails to compile as that version is not available yet.
//getFoo[_3,_0]// 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