Skip to content

Instantly share code, notes, and snippets.

@sellmair
Created May 10, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sellmair/78a4a12ddc785915cc3f470e6deeda46 to your computer and use it in GitHub Desktop.
Save sellmair/78a4a12ddc785915cc3f470e6deeda46 to your computer and use it in GitHub Desktop.
Phantom Types in Kotlin: Modelling a Pipeline Request with static accessor
class Request<T>(
private val image: Image?,
private val timestamp: Timestamp?,
private val measureSettings: MeasureSettings?,
private val preparedImage: Mat?,
private val scaledPreparedImage: Mat?,
private val shapes: List<Shape>?,
private val features: List<Feature>?,
private val people: List<Person>?) {
companion object {
fun imageOf(request: Request<out ReadRawImage>): Image =
request.image ?: throw StageException()
fun timestampOf(request: Request<out ReadTimestamp>): Timestamp =
request.timestamp ?: throw StageException()
fun measureSettingsOf(request: Request<out ReadMeasureSettings>): MeasureSettings =
request.measureSettings ?: throw StageException()
fun preparedImageOf(request: Request<out ReadPreparedImage>): Mat =
request.preparedImage ?: throw StageException()
fun scaledPreparedImageOf(request: Request<out ReadPreparedImage>) =
request.scaledPreparedImage ?: throw StageException()
fun shapesOf(request: Request<out ReadShapes>) =
request.shapes ?: throw StageException()
fun featuresOf(request: Request<out ReadFeatures>) =
request.features ?: throw StageException()
fun peopleOf(request: Request<out ReadPeople>) =
request.people ?: throw StageException()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment