Skip to content

Instantly share code, notes, and snippets.

@andrzejressel
Created January 27, 2018 12:04
Show Gist options
  • Save andrzejressel/f302bc67e11072b5fe2fbab77c244e49 to your computer and use it in GitHub Desktop.
Save andrzejressel/f302bc67e11072b5fe2fbab77c244e49 to your computer and use it in GitHub Desktop.
[at]optics
/**
* Java, because kotlin doesn't allow fields for annotations
*/
public @interface optics {
Type[] value() default {};
public enum Type {
ISO, //data class
LENS, //data class
OPTIONAL, //data class
PRISM //sealed class
}
}
//Generates prism
@optics sealed class NetworkResult {
data class Success(val content: String) : NetworkResult()
object Failure : NetworkResult()
}
//Error: Optional is not valid for sealed class
@optics(OPTIONAL) sealed class NetworkResult2 {
data class Success(val content: String) : NetworkResult2()
object Failure : NetworkResult2()
}
//Generates iso, lens and optional
@optics
data class Company(val name: String)
//Generates only iso
@optics(ISO)
data class Company2(val name: String)
//Error: Prism is not valid for data class
@optics(PRISM)
data class Company3(val name: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment