Skip to content

Instantly share code, notes, and snippets.

@mushtaq
Forked from therealcisse/scala3-is-awesome.scala
Created July 19, 2020 15:25
Show Gist options
  • Save mushtaq/c83fd4486bdc4a324e422dcc9d761fa7 to your computer and use it in GitHub Desktop.
Save mushtaq/c83fd4486bdc4a324e422dcc9d761fa7 to your computer and use it in GitHub Desktop.
// comparing to kotlin version: https://proandroiddev.com/extension-classes-f9dcf5878b71
sealed trait Feature
object SomeFeature extends Feature
trait AppFeatures {
def isEnabled(feature: Feature): Boolean
}
extension (appFeatures: AppFeatures)
inline def apply(op: AppFeatures ?=> Unit): Unit = op(using appFeatures)
object FakeFeatures extends AppFeatures {
def isEnabled(feature: Feature): Boolean = true
}
extension (feature: Feature)(using appFeatures: AppFeatures)
inline def enabled: Boolean = appFeatures.isEnabled(feature)
inline def disabled: Boolean = !appFeatures.isEnabled(feature)
@main def main() = FakeFeatures {
if (SomeFeature.enabled) {
println("Some feature is enabled")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment