Skip to content

Instantly share code, notes, and snippets.

@Aidanvii7
Created July 17, 2020 08:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Aidanvii7/14aeae7981759dbedab83e7f5ecabb32 to your computer and use it in GitHub Desktop.
Save Aidanvii7/14aeae7981759dbedab83e7f5ecabb32 to your computer and use it in GitHub Desktop.
fun main() {
FakeAppFeatures {
if (SomeFeature.enabled) {
println("some feature is enabled!")
}
}
}
sealed class Feature
object SomeFeature : Feature()
interface AppFeatures {
fun isEnabled(feature: Feature): Boolean
}
object FakeAppFeatures : AppFeatures {
override fun isEnabled(feature: Feature): Boolean = true
}
inline class AppFeaturesExtension(val appFeatures: AppFeatures) {
val Feature.enabled: Boolean
get() = appFeatures.isEnabled(this)
val Feature.disabled: Boolean
get() = enabled.not()
}
inline operator fun <T> AppFeatures.invoke(
appFeaturesExtensionContext: AppFeaturesExtension.() -> T
): T = appFeaturesExtensionContext(AppFeaturesExtension(this))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment