Skip to content

Instantly share code, notes, and snippets.

@ilya-g
Last active August 29, 2015 14:18
Show Gist options
  • Save ilya-g/ee05b3fd6e10856cfb64 to your computer and use it in GitHub Desktop.
Save ilya-g/ee05b3fd6e10856cfb64 to your computer and use it in GitHub Desktop.
package syntax.flagEnums
private val TODO: Nothing
get() = throw UnsupportedOperationException()
public open class PatternOptions(public val value: Int = 0) {
// the only meaning declarations
public object CASE_INSENSITIVE : PatternOptions(0x02)
public object COMMENTS : PatternOptions(0x04)
public object MULTILINE : PatternOptions(0x08)
// boilerplate
// ?
public constructor(vararg options: PatternOptions) : this(options.fold(0) { v, value -> v or value.value })
public fun plus(other: PatternOptions): PatternOptions {
if (this.value == 0) return other
if (other.value == 0) return this
return PatternOptions(this.value or other.value)
}
public fun minus(other: PatternOptions): PatternOptions = TODO
public override fun toString(): String = TODO // TODO: Decompose value, substitute name, join with comma
public fun name(): String = TODO // TODO: Only defined for singular name
//TODO: Comparable<PatternOptions>
//TODO: equality members
companion object {
public val values: List<PatternOptions>
get() = TODO
// TODO: Decompose names, substitute value, join with +
public fun valueOf(value: String): PatternOptions = TODO
}
}
fun testOptions() {
fun use(options: PatternOptions) {}
use( PatternOptions() )
use(PatternOptions.CASE_INSENSITIVE)
use(PatternOptions.COMMENTS + PatternOptions.CASE_INSENSITIVE)
use(PatternOptions(PatternOptions.CASE_INSENSITIVE, PatternOptions.MULTILINE))
// compose from value
val p1 = PatternOptions(0x0F)
// maybe val p2 = PatternOptions.valueOf(0x0f)
val p3 = PatternOptions.valueOf("COMMENTS, CASE_INSENSITIVE")
// toggle flag off
val p4 = p3 - PatternOptions.MULTILINE
// type conversion
val v1 = p1.value // or toInt() ?
val n4 = p4.name()
val s3 = p3.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment